Page 1 of 2

Combine several criteria in a search field

Posted: Thu May 08, 2025 8:11 am
by matgarage
Hi

I'm trying to filter the lines in a data grid with a search field.

The data of the data grid is stored in a custom property of the stack, and I put it in a variable "tBaseValue" for the filter.
The variable "rval" is the content of the search field.

My command is :

Code: Select all

filter lines of tBaseValue matching  "*" & rval & "*" into tText
The data grid content is updated with "tText"

I've managed to use the ‘*’ character as a wildcard, but I can't manage to combine several criteria in the search field.
ex : PULP & STRAWBERRY
I want to filter the lines containing ‘PULP’ and those containing ‘STRAWBERRY’.

What is the good syntax to do that ?

Re: Combine several criteria in a search field

Posted: Thu May 08, 2025 11:05 am
by richmond62
How about:

Code: Select all

put "PULP" into PULP
put "STRAWBERRY" into STRWB
filter lines of tBaseValue matching  PULP & rval & STRWB into tText

Re: Combine several criteria in a search field

Posted: Thu May 08, 2025 1:53 pm
by matgarage
Hello Richmond62

‘Rval’ is the value of what I want to search for in the datagrid rows.
In your script, if my search value is ‘white’ I would get:
PULPwhiteStrawberry
Right ?

In the dictionary, the ‘filter’ function offers the following options:
* and ?, and others focused on individual characters.
What I'm looking for is a way of searching with an ‘and/or’ option between PULP and STRAWBERRY.
The result should be all lines containing PULP and/or STRAWBERRY.

Re: Combine several criteria in a search field

Posted: Thu May 08, 2025 2:24 pm
by richmond62

Code: Select all

filter lines of tBaseValue matching  "*" & PULP & "*" into tText and filter lines of tBaseValue matching  "*" & STRAWBERRY & "*" into tText

Re: Combine several criteria in a search field

Posted: Thu May 08, 2025 2:59 pm
by dunbarx
Richmond.

I do not think the "filter" command can be forced to do what you posted. The idea is sound, but LC may not accept that syntax.

Matgarage:
You may need two passes, so do this;

Code: Select all

on mouseUp
   filter lines of fld 1 matching "*" & "PULP" & "*" into tText
   put tText into finalText
   filter lines of fld 1 matching "*" & "STRAWBERRY" & "*" into tText
   put  return & tText after finalText
end mouseUp
Craig

Re: Combine several criteria in a search field

Posted: Thu May 08, 2025 4:19 pm
by matgarage
You're right, Craig.

I'll have to rethink my strategy.
My script systematically retrieves the original content in the custom Properties to apply the filter with each character entered.
But that's not possible for a multi-criteria search.

I'm going to think about a simple integration for the user, for example a second search field — Refinement for Severance lovers ;-) — that retrieves the content of the datagrid and not that of the custom property.

Thank you

Re: Combine several criteria in a search field

Posted: Thu May 08, 2025 10:13 pm
by dunbarx
Matgarage .

Code: Select all

...for example a second search field
The user must define the several terms anyway, however you make that happen. Once you have those search terms, use a repeat loop to build them all into the new "found" list. For example, put your stack custom property into a variable, say "rawData". Put the search terms in, say in a return delimited variable like "searchTermList:

Code: Select all

on mouseUp
   put yourCustomProperty into rawData
   repeat with y = 1 to the number of lines of searchTermList
      filter lines of rawData matching "*" & line y of searchTermList & "*" into temp
      put temp & return after foundList
   end repeat
   answer foundList
end mouseUp
This is also an example of how useful the "repeat with.." variation ia as opposed to the "repeat for.." variation. It is nice to have a built-in running index.

Craig

Re: Combine several criteria in a search field

Posted: Fri May 09, 2025 7:40 pm
by jacque
Here's a way to search for multiple terms using the filter function. This assumes that search terms are separated by spaces but items would work too with minor modifications. It doesn't matter where the data is stored, in my test I used a field but a property would work fine. The main thing is the filter function using a regex construction.

Code: Select all

on mouseUp
  put fld 1 into tData -- this can come from anywhere 
  put word 1 to -1 of fld "search" into tTerms -- remove any bracketing spaces
  replace space with "|" in tTerms
  filter tData with regex pattern tTerms into tResult
  put tResult -- display it wherever you want 
end mouseUp

Re: Combine several criteria in a search field

Posted: Sat May 10, 2025 12:27 pm
by stam
dunbarx wrote:
Thu May 08, 2025 2:59 pm
I do not think the "filter" command can be forced to do what you posted. The idea is sound, but LC may not accept that syntax.
That's not quite right.

As Jacqui points out, you can use the regex version of the filter command:

Code: Select all

filter <container> with regex pattern <regex string> [into <newContainer>]
using the pipe symbol "|" which means "OR" in regex so you could replace whatever separator of terms you define as for the users with this, but be aware that spaces are important in regex statments unless you use the the x flag to ignore white space within the regex statement.

If anyone wants to learn more about regex, I wrote a short wiki tutorial slanted towards LiveCode here.



Alternatively you can use the where version of the filter command:

Code: Select all

filter <container> where each <criterion 1> OR each <criterion 2> [OR each <criterion n>] [into <newContainer>]
You can use a do merge() statement to build this dynamically for a variable number of search criteria - but in truth its just simpler to use regex if you can't know how many search criteria will be entered.

Re: Combine several criteria in a search field

Posted: Sat May 10, 2025 6:26 pm
by dunbarx
I do not think the "filter" command can be forced to do what you posted. The idea is sound, but LC may not accept that syntax.
At least I left room to eat those words.

Craig

Re: Combine several criteria in a search field

Posted: Sat May 10, 2025 7:44 pm
by richmond62
Screenshot 2025-05-10 at 21.41.55.jpg

Re: Combine several criteria in a search field

Posted: Tue May 13, 2025 4:50 pm
by matgarage
Whouaou

I've been away for a few days, so I'm delighted that my question didn't find Livecode's limits.
Too busy today to get back to coding, I'm saving myself a slot tomorrow to try out these solutions.

However, I hope that ‘REGEX’ won't give me nightmares tonight...

Re: Combine several criteria in a search field

Posted: Tue May 13, 2025 5:44 pm
by dunbarx
I have used simple regex constructions, along with wildCards, mostly with the "filter" command. But I am skittish like you.

So just be old fashioned (like me) and use the simple repeat loop a few posts above. Again:

Code: Select all

on mouseUp
   put yourCustomProperty into rawData
   repeat with y = 1 to the number of lines of searchTermList --searchTermList is your list of words to find 
      filter lines of rawData matching "*" & line y of searchTermList & "*" into temp
      put temp & return after foundList
   end repeat
   answer foundList
end mouseUp
I also think would be easier to debug.

As long as we are being old-fashioned, know that you could use the lineOffset function nearly as easily.

Craig

Re: Combine several criteria in a search field

Posted: Wed May 14, 2025 1:56 pm
by matgarage
I was really looking forward to putting your advice into practice.
Nice, all three solutions work.
I've adapted the jaque script a little to work directly in my search field.

In the end, REGEX the Jaque way isn't so complicated. The pipe symbol is not beyond my skills.

It's the simplest and most effective solution for achieving my goal.

Here is the final script :

Code: Select all

on keyDown theKey 
   put the BaseJobs of this stack into tData 
   put theKey after fld "fRecherche2"
   put fld "fRecherche2" into tSearch
   put word 1 to -1 of tSearch into tTerms
   replace space with "|" in tTerms
   filter tData with regex pattern tTerms into tResult
   set the DGtext of group "DG_PrintOS" to tResult
end keyDown
Thank you for your help

Re: Combine several criteria in a search field

Posted: Wed May 14, 2025 2:06 pm
by stam
Regex is an important tool to be familiar with - it makes lots of complex stuff simpler. It’s scary to approach because of the 1-char tokens that can be confusing, but the internet is full of tutorials and examples and practically all questions are already answered to some extent.

The regex for LiveCode primer I linked above has some links to full tutorials if I remember correctly (haven’t looked at it in a while).

I would also highly recommend https://regex101.com.
It’s probably the best regex IDE and it’s free. Great way to test regex expressions until you get them working. It’s my go-to tool any time I’m working with regex…