finding an array element
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: finding an array element
Right on.
"Each" is one of the most endearing gadgets in the whole of the language.
Craig
"Each" is one of the most endearing gadgets in the whole of the language.
Craig
-
Simon Knight
- Posts: 930
- Joined: Wed Nov 04, 2009 11:41 am
Re: finding an array element
Bump!
I have an array with text keys and more text as the values. How do I filter for two text values in an element?
I'm seeking an AND statement but can't find one at the moment.
For example lets say that MyArray contains the following:
I want to filter the array based on elements that contain "brown" and "dog" which should result in an array with a single entry.
Thanks
Simon
I have an array with text keys and more text as the values. How do I filter for two text values in an element?
I'm seeking an AND statement but can't find one at the moment.
For example lets say that MyArray contains the following:
Code: Select all
MyArray["First Key is text"]["Now is the time for all good men to come to the aid of the party"]
MyArray["Second Key is also text"]["The quick brown fox jumps over the lazy dog"]
MyArray["Third Key is also text"]["The quick brown fox snarls at the lazy cat"]I want to filter the array based on elements that contain "brown" and "dog" which should result in an array with a single entry.
Thanks
Simon
best wishes
Skids
Skids
Re: finding an array element
Hi Simon,
I may well leb be proven wrong by more knowledge people, but I think the issue is the structure of your array, which makes it unsuitable to use 'filter' with. As you've structured your array, I think you'd need to iterate through it to locate your search terms rather than use "filter" because you can't use "each" since every top level key is named differently.
If you wanted to make your life easier by using filter, you'd probably need to structure your array differently. For example, this works:
I may well leb be proven wrong by more knowledge people, but I think the issue is the structure of your array, which makes it unsuitable to use 'filter' with. As you've structured your array, I think you'd need to iterate through it to locate your search terms rather than use "filter" because you can't use "each" since every top level key is named differently.
If you wanted to make your life easier by using filter, you'd probably need to structure your array differently. For example, this works:
Code: Select all
local MyArray, tFilteredA
// Create numerically keyed array
put "First Key is text" into MyArray[1]["Level1"]
put "Now is the time for all good men to come to the aid of the party" into MyArray[1]["Level2"]
put "Second Key is also text"into MyArray[2]["Level1"]
put "The quick brown fox jumps over the lazy dog" into MyArray[2]["Level2"]
put "Third Key is also text" into MyArray[3]["Level1"]
put "The quick brown fox snarls at the lazy cat" into MyArray[3]["Level2"]
// FILTER the array
filter elements of myArray where each["Level2"] contains "brown" and each["Level2"] contains "dog" into tFilterA
breakpoint -- you can now inspect the found array in tFilterA - only one element returned
Last edited by stam on Tue Dec 02, 2025 3:41 am, edited 1 time in total.
Re: finding an array element
If you want to stick with the format of your current array, you could iterate:
Code: Select all
on mouseUp
local MyArray, tFilterA, tKeys, tKey, tSubKey
-- Create Array structure
put empty into MyArray["First Key is text"]["Now is the time for all good men to come to the aid of the party"]
put empty into MyArray["Second Key is also text"]["The quick brown fox jumps over the lazy dog"]
put empty into MyArray["Third Key is also text"]["The quick brown fox snarls at the lazy cat"]
-- Search the array
put the keys of MyArray into tKeys
repeat with x = 1 to the number of lines in tKeys
put line x of tKeys into tKey
put the keys of MyArray[tKey] into tSubKey
if "dog" is in tSubKey and "brown" is in tSubKey then
put MyArray[tKey] into tFilterA[tKey]
end if
end repeat
breakpoint -- tFilterA contains a single array element
end mouseUp
Re: finding an array element
Simon:
Here is a possible solution. It was initially suggested by jacque.
See
viewtopic.php?f=8&t=39716#p234042
Bob
Here is a possible solution. It was initially suggested by jacque.
See
viewtopic.php?f=8&t=39716#p234042
Bob
Re: finding an array element
I think the issue is which container you’re searching with either regex or “where”.bobcole wrote: ↑Tue Dec 02, 2025 5:48 pmSimon:
Here is a possible solution. It was initially suggested by jacque.
See
viewtopic.php?f=8&t=39716#p234042
Bob
Is the container the entire key and its subkeys?
My impression is probably not - in my mind the “container” that will be searched with any method is the subkey itself, not the entire key and it’s all its subkeys, although would be very happy to be proven wrong as the latter would actually be very useful.
And you can’t use the form each[subkey] as that’s named differently in every key with the example given.
Hence my first recommendation was to restructure the array to name each subkey so you can use the each keyword - then the search becomes a 1-line filter command.
However if each key and each subkey are named differently I think you would need to iterate through the whole array.
Re: finding an array element
I am missing something, as usual.
Arrays and the "filter" command notwithStanding, and assuming the OP original text is in a field 1, what is missing from the solution:
Craig
Arrays and the "filter" command notwithStanding, and assuming the OP original text is in a field 1, what is missing from the solution:
Code: Select all
on mouseUp
get fld 1
repeat for each line tLine in it
if "brown" is in tLine or "dog" is in tLine then next repeat
put tLine & return after temp
end repeat
answer temp
end mouseUpRe: finding an array element
The bit you’re missing is Simon’s post on this page where he wants to filter an array in the form:
And actually what he asking for is a way to filter the keys…Simon Knight wrote: ↑Mon Dec 01, 2025 9:05 pmI have an array with text keys and more text as the values. How do I filter for two text values in an element?
I'm seeking an AND statement but can't find one at the moment.
For example lets say that MyArray contains the following:Code: Select all
MyArray["First Key is text"]["Now is the time for all good men to come to the aid of the party"] MyArray["Second Key is also text"]["The quick brown fox jumps over the lazy dog"] MyArray["Third Key is also text"]["The quick brown fox snarls at the lazy cat"]
I want to filter the array based on elements that contain "brown" and "dog" which should result in an array with a single entry.
