Use the string-oriented search features of REXX where possible.
Example: Suppose you've got a file in the following format:
Doe John , 01.03.65, Engineer Miller Franklin, 03.04.67, Butcher Gernot Martha, 06.05.65, Saleswoman
To read this file into a compound variable and search an entry you can use the following code:
/* */ /* sample for using compound variables - slow method */ /* */ /* ------------------------ variable section ------------------------ */ /* name of the input file */ inFile = "pdata.txt" /* name for the output file */ outFile = "data.new" /* compound variable for the file contents */ persons. = "" persons.0 = 0 /* ------------------------- read the file -------------------------- */ /* read the file */ do i = persons.0+1 while lines( inFile ) <> 0 persons.i = lineIn( inFile ) end /* do while lines( inFile ) <> 0 */ persons.0 = i-1 /* ------------------------ search an entry ------------------------- */ /* compound variable with the entries to search */ entriesToSearch.0 = 2 entriesToSearch.1 = "Miller Franklin" entriesToSearch.2 = "MILLER Franklin" do j = 1 to entriesToSearch.0 curSearchEntry = entriesToSearch.j found = 0 do i = 1 to persons.0 while found=0 if pos( curSearchEntry, persons.i ) = 1 then found = i end /* do i = 1 to persons.0 while found=0 */ if found <> 0 then say "Entry <" || curSearchEntry || "> found: " || , persons.found else say "Entry <" || curSearchEntry || "> NOT found!" end /* do j = 1 to entriesToSearch.0 */ /* --------- write the compound variable back into a file ----------- */ do i = 1 to persons.0 /* ignore deleted entries */ if persons.i <> "" then call LineOut outFile, persons.i end /* do i = 1 to persons.0 */
You can use this method -- but it's not very fast (especially if you've got hundreds or thousands of entries).
A better method to do this is: Use a key field as name for the tail. Example:
/* */ /* sample for using compound variables - faster method */ /* */ /* ------------------------ variable section ------------------------ */ /* name of the input file */ inFile = "pdata.txt" /* name for the output file */ outFile = "data.new" /* compound variables for the file contents */ persons. = "" persons.0 = 0 /* "index" variable */ personsIndex = "" /* separator for the index variable */ separator = "00"x /* ------------------------- read the file -------------------------- */ do i = 1 while lines( inFile ) <> 0 parse value lineIn( inFile ) with entryKey "," entryValue /* in this example the key field is the name of */ /* the person */ entryKey = strip( entryKey ) /* check for duplicate keys and create the stem */ /* entry if possible */ if persons.entryKey <> "" then do /* duplicate key found */ say "Error: Duplicate key <" || entryKey || "> in the file!" end /* if persons.entryKey <> "" then */ else do /* save the key in the "index" variable */ personsIndex = personsIndex || entryKey || separator persons.entryKey = entryValue end /* else */ end /* do i = 1 while lines( inFile ) <> 0 */ persons.0 = i-1 /* ------------------------ search an entry ------------------------- */ /* compound variable with the entries to search */ entriesToSearch.0 = 2 entriesToSearch.1 = "Miller Franklin" entriesToSearch.2 = "MILLER Franklin" do i = 1 to entriesToSearch.0 curSearchEntry = entriesToSearch.i if persons.curSearchEntry <> "" then say "Entry <" || curSearchEntry || "> found:" || , persons.curSearchEntry else say "Entry <" || curSearchEntry || "> NOT found!" end /* do i = 1 to entriesToSearch.0 */ /* --------- write the compound variable back into a file ----------- */ do while personsIndex <> "" /* get the next variable name */ parse var personsIndex curPerson (separator) personsIndex /* ignore deleted entries */ if persons.curPerson <> "" then call LineOut outFile, curPerson || "," || persons.curPerson end /* do while personIndex <> "" */
Note that you can use any character for the tail of a compound variable. But keep in mind, that using this method the name of the tail is case sensitive! (see also Using variables for the tail)