The NthField function is great for parsing text that is very structured
(like columns of tab-delimited data for example). But it can also be
used to parse data that is stored in a more complex format. Consider
the following XML data:
<FirstName>Geoff</FirstName>
<LastName>Perlman</LastName>
How would you retrieve the value in the last name tag? It can be done
quite easily with the NthField function. Here's the code that does it.
Assume for this example that the XML text above is in a string called
"sourceText":
results = NthField(sourceText, "<LastName>", 2)
results = NthField(results, "</LastName>", 1)
The first line uses the <LastName> tag as the delimiter and 2 for the
field number. Thus everything that follows the tag (Perlman</LastName>)
is returned:
Perlman</LastName>
The second line uses this result, uses the end tag (</LastName>) as
the delimiter and asks for field 1 ("Perlman" in this case).
This is a simple and unconventional use of the NthField function for
parsing text.
This example was inspired by Mike Bailey.
--
Geoff Perlman
President and CEO
REAL Software, Inc.
|