Filtering only specific items from Array
Posted: Wed Dec 19, 2012 6:29 pm
Here is my challenge. I have created an array based on content that is in an XML tree. I now want to filter the array to create a new array with only specific elements from the array.
Array
date[1] 2012-12-12
value[1] 10.4
new[1] 0 NOTE: either 0 or 1 to indicate if node is new entry into array
....
date[2] 2012-12-12
value[2] 12.3
new[2] 1
....
date[3] 2012-12-12
value[3] 11
new[3] 1
....
I want to create a function that will create a new array with only the elements that have the new flag set to 1.
The challenge is that I want to create a generic function that does not specifically know what the labels are for all the keys in the array.
If a new element label is added to the array I want the function to determine the list of keys from the array.
Here is my code so far, but I'm doing something wrong because I'm not getting a unique list of keys or I'm not using the key list properly.
Thanks for the help on this challenge.
Array
date[1] 2012-12-12
value[1] 10.4
new[1] 0 NOTE: either 0 or 1 to indicate if node is new entry into array
....
date[2] 2012-12-12
value[2] 12.3
new[2] 1
....
date[3] 2012-12-12
value[3] 11
new[3] 1
....
I want to create a function that will create a new array with only the elements that have the new flag set to 1.
The challenge is that I want to create a generic function that does not specifically know what the labels are for all the keys in the array.
If a new element label is added to the array I want the function to determine the list of keys from the array.
Here is my code so far, but I'm doing something wrong because I'm not getting a unique list of keys or I'm not using the key list properly.
Code: Select all
-- convert the information from the XML string into an ARRAY
split theDataArray by "|" and tab
-- Look through each of the nodes to see if the NEW flag is set
put 1 into tNewItem
put 1 into tItem
put the keys of theDataArray into tKeys
repeat while tItem < tNodeCount
if theDataArray["new["& tItem &"]"] = "1" then
repeat with i=1 to the number of items of tKeys
put item i of tKeys into tItemName
put theDataArray[ tItemName&"["& tItem &"]"] into theNewArray[tItemName&"["& tNewItem &"]"]
end repeat
add 1 to tNewItem
end if
add 1 to tItem
end repeat