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!
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.
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’.
‘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.
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
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.
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:
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.
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.
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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
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:
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:
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.
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...
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.
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.
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
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…