Filter array by multiple keys

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Filter array by multiple keys

Post by stam » Sun Jul 04, 2021 4:09 am

Hi all,
I know this works:

Code: Select all

filter elements of tArray where each["key"] is "xyz"
However i don't know if it's possible to filter by several keys?
When trying to filter an array checking a value in 2 different keys, this does not work:

Code: Select all

filter elements of tArray where each["key1"] = "xyz" or each["key2"] = "xyz"
Is it possible to do this? if so, what is the correct syntax?

Many thanks
Stam

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: Filter array by multiple keys

Post by rkriesel » Sun Jul 04, 2021 4:46 am

stam wrote:
Sun Jul 04, 2021 4:09 am
Is it possible to do this? if so, what is the correct syntax?
Yes: yours.
At least, it works for me.

Code: Select all

on mouseUp
   local t
   put "xyz" into t[1]["key1"]
   put "abc" into t[2]["key1"]
   put "xyz" into t[3]["key2"]
   filter elements of t where each["key1"] = "xyz" or each["key2"] = "xyz"
   breakpoint
end mouseUp
How'd you get it to fail?
-- Dick

stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Filter array by multiple keys

Post by stam » Sun Jul 04, 2021 12:37 pm

Hrmph… well in actuality the code in my app was searching for 3 keys “not empty”. Mind u late night coding, may have done something silly.
Will have a go again when back from work..,.

stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Filter array by multiple keys

Post by stam » Sun Jul 04, 2021 8:50 pm

rkriesel wrote:
Sun Jul 04, 2021 4:46 am
Yes: yours.
How'd you get it to fail?
yep confirmed... me doing silly stuff at 3 am... ignore ;)

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”