Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: parse question

From: joel:neely:fedex at: 26-Jun-2001 18:42

Hi, Brett, I must agree with Graham on this one. A zero-length string is a completely legitimate value, but most definitely is *not* the same thing as NONE! Brett Handley wrote:
> Hi Graham, > > > If parse always returns a string >
I think he meant "sets variables to string! values using COPY" instead of "returns"...
> > shouldn't > > > > parse {a=""} [ thru {a="} copy test to {"} ] > > > > 'test now be an empty string rather than type none! ? > > > > I guess is depends on how you see it, either as "nothing to > copy" (none!) or "copied a string of zero length" (empty). > Also, I don't see that it makes a lot of difference - except > for perhaps some more if statements in some circumstances. >
Any time you wish to build a new string from the parsed substrings, or print their content, you have a problem with NONE! instead of an empty string. For example, given: addr1: "John Doe/123 Lonely St/Suite 16/Los Angeles/CA/90210" addr2: "Joe Doaks/321 Hilltop Ln//Green Mtn/MA/02187" print-address-label: function [ s [string!] ][ addr-name addr-line1 addr-line2 addr-city addr-state addr-ZIP ][ parse/all s [ copy addr-name to "/" skip copy addr-line1 to "/" skip copy addr-line2 to "/" skip copy addr-city to "/" skip copy addr-state to "/" skip copy addr-ZIP to end ] print [ addr-name newline addr-line1 newline addr-line2 newline addr-city " " addr-state " " addr-ZIP newline ] ] ... we get ...
>> print-address-label addr1
John Doe 123 Lonely St Suite 16 Los Angeles CA 90210
>> print-address-label addr2
Joe Doaks 321 Hilltop Ln none Green Mtn MA 02187 Clearly the address label should contain a blank line between 321 Hilltop Ln and "Green Mtn Ma 02187". This is only a trivial example; I'm not suggesting this is the best way to represent addresses or print labels! If the example is too trite, just pretend that the syntax of the address were much more complex, with various punctuation, etc. I've done a tremendous amount of text-flogging over the years, converting address lists, reformatting data files among a variety of representations, etc. In such applications, it's very common to parse out the pieces of one format and immediately construct a new string or write/print using some combination of the just-parsed text fields. Yes, I know it's possible to follow the PARSE statement with a list of "fix-the-empty-strings" statements, as in parse/all s [ ;... ] addr-name: any [addr-name ""] addr-line1: any [addr-line1 ""] addr-line2: any [addr-line2 ""] ;... print [ ;... ] (or even to make a fix-up function and call it on all of the fields), but all that bother hardly seems in keeping with "make easy things easy and hard things possible" IMHO. -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com