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

[REBOL] Re: Perl is to stupid to understand this 1 liner.

From: joel:neely:fedex at: 14-Dec-2001 6:43

Hi, Carl, Close, but no cigar!
> > 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. -jn-
> It could've been a bit shorter by making duplicate 1-character words > for 'parse and 'length but we want to keep it "readable", right? (: >
Right! ;-) Also, including the definitions for P = PARSE and L = LENGTH would just about eat up the 20-character savings anyway, and the unspoken "fairness" rule is that any definitions outside the standard installation features of the language would be included. -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;