Page 2 of 2
Re: finding an array element
Posted: Tue Mar 22, 2022 9:20 pm
by dunbarx
Right on.
"Each" is one of the most endearing gadgets in the whole of the language.
Craig
Re: finding an array element
Posted: Mon Dec 01, 2025 9:05 pm
by Simon Knight
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:
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
Re: finding an array element
Posted: Tue Dec 02, 2025 3:09 am
by stam
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:
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
Re: finding an array element
Posted: Tue Dec 02, 2025 3:33 am
by stam
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
Posted: Tue Dec 02, 2025 5:48 pm
by bobcole
Simon:
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
Posted: Tue Dec 02, 2025 5:59 pm
by stam
I think the issue is which container you’re searching with either regex or “where”.
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
Posted: Tue Dec 02, 2025 9:04 pm
by dunbarx
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:
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 mouseUp
Craig
Re: finding an array element
Posted: Tue Dec 02, 2025 10:33 pm
by stam
dunbarx wrote: Tue Dec 02, 2025 9:04 pm
I am missing something, as usual.
The bit you’re missing is Simon’s post on this page where he wants to filter an array in the form:
Simon Knight wrote: Mon Dec 01, 2025 9:05 pm
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.
And actually what he asking for is a way to filter the keys…
Re: finding an array element
Posted: Thu Dec 04, 2025 9:26 pm
by Simon Knight
Thanks for all your posts.
#Stam, I will restructure my array in the manor you suggest.
Thanks
Simon
Re: finding an array element
Posted: Wed Dec 10, 2025 2:44 am
by citybrawl
I using
with
too, much cleaner and more readable than traditional loops.
Re: finding an array element
Posted: Thu Dec 11, 2025 3:06 am
by bobcole
To effect an AND, a command may be run twice.
Code: Select all
filter elements of MyArray with regex "fox"
filter elements of MyArray with regex "dog"
I have a simple LiveCode example in the attached file.
Bob
Re: finding an array element
Posted: Fri Dec 12, 2025 9:28 pm
by stam
bobcole wrote: Thu Dec 11, 2025 3:06 am
To effect an AND, a command may be run twice.
Code: Select all
filter elements of MyArray with regex "fox"
filter elements of MyArray with regex "dog"
First off I'm a regex fan. I would use regex for everything if I could.
But
does this work with elements that have subkeys? And, importantly, does it work with the array architecture
Simon provided above?
As a test, I tried your code with Simon's array above. It just returns an empty array. Same thing happens with my proposal to Simon to restructure his array so that any complex filter with 'and', 'or', 'contains', etc, becomes a 1-liner (see
my post above for details).
The only way your code works, as far as I can see, with a simple array without subkeys, eg
Code: Select all
put "Now is the time for all good men to come to the aid of the party" into MyArray[1]
put "The quick brown fox jumps over the lazy dog" into MyArray[2]
put "The quick brown fox snarls at the lazy cat" into MyArray[3]
Don't get me wrong - I'd be
more than happy to to be corrected and be able to search all elements, keys and subkeys as a single container with a 1-line
filter regex command, but as I understand arrays (and I may well be very wrong), that won't work...