tips
[Top] [All Lists]

REALbasic Tip: A different way to use NthField for text parsing

To: REALbasic-tips at lists dot realsoftware dot com
Subject: REALbasic Tip: A different way to use NthField for text parsing
From: Geoff Perlman <geoff at realsoftware dot com>
Date: Mon, 21 Jun 2004 16:38:16 -0500
Cc:
Delivered-to: REALbasic-tips at lists dot realsoftware dot com
List-help: <mailto:realbasic-tips-request@lists.realsoftware.com?subject=help>
List-id: REALbasic Tips <realbasic-tips.lists.realsoftware.com>
List-post: <mailto:realbasic-tips@lists.realsoftware.com>
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.

<Prev in Thread] Current Thread [Next in Thread>
  • REALbasic Tip: A different way to use NthField for text parsing, Geoff Perlman <=