finding an array element

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10423
Joined: Wed May 06, 2009 2:28 pm

Re: finding an array element

Post by dunbarx » Tue Mar 22, 2022 9:20 pm

Right on.

"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

Post by Simon Knight » Mon Dec 01, 2025 9:05 pm

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
best wishes
Skids

stam
Posts: 3155
Joined: Sun Jun 04, 2006 9:39 pm

Re: finding an array element

Post by stam » Tue Dec 02, 2025 3:09 am

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
 
Last edited by stam on Tue Dec 02, 2025 3:41 am, edited 1 time in total.

stam
Posts: 3155
Joined: Sun Jun 04, 2006 9:39 pm

Re: finding an array element

Post by stam » Tue Dec 02, 2025 3:33 am

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

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 174
Joined: Tue Feb 23, 2010 10:53 pm

Re: finding an array element

Post by bobcole » Tue Dec 02, 2025 5:48 pm

Simon:
Here is a possible solution. It was initially suggested by jacque.
See
viewtopic.php?f=8&t=39716#p234042
Bob

stam
Posts: 3155
Joined: Sun Jun 04, 2006 9:39 pm

Re: finding an array element

Post by stam » Tue Dec 02, 2025 5:59 pm

bobcole wrote:
Tue Dec 02, 2025 5:48 pm
Simon:
Here is a possible solution. It was initially suggested by jacque.
See
viewtopic.php?f=8&t=39716#p234042
Bob
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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10423
Joined: Wed May 06, 2009 2:28 pm

Re: finding an array element

Post by dunbarx » Tue Dec 02, 2025 9:04 pm

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

stam
Posts: 3155
Joined: Sun Jun 04, 2006 9:39 pm

Re: finding an array element

Post by stam » Tue Dec 02, 2025 10:33 pm

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…

Post Reply