Purpose Select table rows that match the value of a regular expression
Regular expressions while not intuitive are nonetheless a
powerful tool when selecting strings; many useful examples and
tutorials for regular expressions can be found on the internet.
Solution
We use
the
match= and
-regex options of the
ReadRowcommand. To show the effect
of the following examples we use a table of cities similar to the
example in the previous article:
GuiXT
// create structured variable
CreateStructure V[cityinfo] city latitude longitude _
country countrycode population
// table variable with all cities from .csv file
CreateTable V[allcities] include=V[cityinfo]
// create structure for city search
CreateStructure V[citysearch] city
We search a specific city "xxx" using the commands
Set V[citysearch.city] "xxx"
ReadRow V[cityinfo] table=V[allcities] _
match=V[citysearch] -regex _
returnindex="row"
Let's assume that we have the following city names in our table:
1 Park Grove 2 Spring Hill 3 Spring Lake 4
Spring Lake Park 5 Spring Valley 6 7
Springboro 8 Springdale 9 Springfield 10 Springhill
12080 11 Hill City 12 Dale
Sample calls and results:
Set
V[citysearch.city]
"Lake"
ReadRow ... Returns row 3, Spring Lake
Set
V[citysearch.city]
"lake"
ReadRow ... Returns row -1, not found
Set
V[citysearch.city]
"lake"
ReadRow ...
-ignoreCase Returns row 3, Spring Lake
Set
V[citysearch.city]
"hill"
ReadRow ...
Returns row 10, Springhill
Set
V[citysearch.city]
"^Hill"
ReadRow ... Returns row 11, Hill
City (^ means: start of string)
Set
V[citysearch.city]
"o$"
ReadRow ...
Returns row 7,
Springboro ($ means: end of string)
Set
V[citysearch.city]
"DALE"
ReadRow ...
-ignoreCase Returns row 8, Springdale
Set
V[citysearch.city]
"^DALE"
ReadRow ...
-ignoreCase Returns row 12, Dale
Set
V[citysearch.city]
"^$"
ReadRow ... Returns row 6 (empty string)
Same as Set
V[citysearch.city] ""
ReadRow ...
Set
V[citysearch.city]
"city|valley"
ReadRow ...
Returns row 5, Spring valley
(| means OR)
Set
V[citysearch.city]
"[0-9]"
ReadRow ...
Returns row 10, Springhill
12080 ([0-9] means: contains at least one digit)