[REBOL] Re: Perl is to stupid to understand this 1 liner.
From: carl:cybercraft at: 15-Dec-2001 22:52
On 15-Dec-01, Joel Neely wrote:
> Hi, Carl,
Hi Joel,
> Close, but no cigar!
Aw, gee...
>> I can protect everybody's phone numbers from lurking telemarketers
>> with the following one-liner in Perl:
>>> 8<----------
>>> # perl -p -e 's/\b(\d{3}-)?\d{3}-\d{4}/####/g' memo.txt
>>> 8<----------
> I should confess to an obvious typo; the above expression should
> have read
> s/\b(\d{3}-)?\d{3}-\d{4}\b/####/g
> of course! How silly of me!
>>> Anyone is welcome to propose a minimalist solution in REBOL!
>>
>> A two-liner is the best I can do Joel...
>>
>> rebol[]foreach n parse/all f: read %memo.txt " ^/(),."[if 1 <
>> length?
>> parse/all n "-"[change/part find f n "####" length? n]]print f
>>
> The meaning of the Perl regular expression is very specific:
> s/ # substitute for this pattern...
> \b # boundary (e.g. whitespace or beginning of line)
> ( # begin subpattern
> \d{3} # exactly three digits
> - # followed by a hyphen
> )? # end subpattern and make it optional
> \d{3} # exactly three digits
> - # followed by a hyphen,
> \d{4} # exactly four more digits, and
> \b # a boundary (whitespace or end of line)
> /####/gx; # ... four octothorps wherever possible
> Other occurrences of hyphens are not relevant. (The above is also
> legal Perl, BTW.) This means that if the memo reads:
> 8<----------
> Ms. Antoinette,
> On 14-Dec-2001 I spoke with George Washington at 555-1212
> about our pending contract. He referred me to Ben Franklin
> (800-555-1111) of their technical support department. Ben
> said that they were testing their latest release (described
> in the letter from Albert Jones-Smythe sent on 12-01-2001)
> on WhizBangOS version 17.3 as we had requested in our memo
> of 28-Nov-2001, and that he would have our answer tomorrow.
> Ben also said that their lead developer, Betsy Ross, would
> like to talk to you about the use of complex numbers in the
> SystemSleepFor function. You may call her office at
> 123-4576; her cell phone is 987-6543; her pager is 111-1111.
> She is very eager to describe this new feature.
> Sincerely,
> Thomas Paine
> (8<----------
> the Perl one-liner produces:
> 8<----------
> Ms. Antoinette,
> On 14-Dec-2001 I spoke with George Washington at ####
> about our pending contract. He referred me to Ben Franklin
> (####) of their technical support department. Ben
> said that they were testing their latest release (described
> in the letter from Albert Jones-Smythe sent on 12-01-2001)
> on WhizBangOS version 17.3 as we had requested in our memo
> of 28-Nov-2001, and that he would have our answer tomorrow.
> Ben also said that their lead developer, Betsy Ross, would
> like to talk to you about the use of complex numbers in the
> SystemSleepFor function. You may call her office at
> ####; her cell phone is ####; her pager is ####.
> She is very eager to describe this new feature.
> Sincerely,
> Thomas Paine
> 8<----------
> but the REBOL code above does this instead:
> 8<----------
> Ms. Antoinette,
> On #### I spoke with George Washington at ####
> about our pending contract. He referred me to Ben Franklin
> (####) of their technical support department. Ben
> said that they were testing their latest release (described
> in the letter from Albert #### sent on ####)
> on WhizBangOS version 17.3 as we had requested in our memo
> of ####, and that he would have our answer tomorrow.
> Ben also said that their lead developer, Betsy Ross, would
> like to talk to you about the use of complex numbers in the
> SystemSleepFor function. You may call her office at
> #### her cell phone is #### her pager is ####.
> She is very eager to describe this new feature.
> Sincerely,
> Thomas Paine
> 8<----------
> Notice that the dates and hyphenated last name were blotted out
> as well as the phone numbers. Adding the necessary tests for
> character class and length would make the REBOL version
> noticeably longer.
Well, you didn't say it was for any old phone numbers in any old text,
did you? Will your Perl script get this right for instance...
{New Zealand Telecom's phone numbers generally include spaces as can
be seen by a quick look at http://www.yellowpages.co.nz/ (Search for
hotel
or the like.) So these are standard NZ phone numbers...
National: 1-2-345 6789
Local: 345 6789
0800: 0800 123 456
}
? (:
But anyway, the following will parse your 3-4/3-3-4 phone syntax (I
think)...
rebol[]c: charset "0123456789" parse/all f: read %memo.txt[some
[a: 1 2[3 c "-"]4 c b:(change/part a "####" b) | skip]]print f
and it's noticably (6 characters:) shorter (not longer!) than my
previous version. (:
I couldn't have done it without Petr's example though, as what I was
trying wasn't working. (: But I now know a lot more about parsing
than I did yesterday...
--
Carl Read