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: carl:cybercraft at: 16-Dec-2001 12:20

On 15-Dec-01, Joel Neely wrote:
> Of course, I could define variables to hold the parts of the RE > and give them mnemonic names ... > 8<---------- > #!/usr/bin/perl -w > my $areacode = '(\d{3}-)?'; # 3 digits and hyphen, optional > my $exchange = '\d{3}-'; # 3 digits and hyphen > my $localine = '\d{4}'; # 4 digits > my $phonenbr = "$areacode$exchange$localine"; > while (<>) { # loop over all input > s/\b$phonenbr\b/####/gx; # hiding phone numbers in each line > print; # and print the line > } > 8<---------- > ... but anyone who knows Perl will see that I had to do something > subtle to make that work. > Perhaps that would be more readable to some?
Even without the comments it at least lets you know it's doing something with phone numbers, though you wouldn't pick up they were being blanked out just by the "####" in the line.
> What would one do > in REBOL to make the PARSE rules more obvious to someone who > doesn't speak REBOL as a native?
Well, this is how we're more or less supposed to write REBOL code... rebol[] blank-phone-numbers: func [ {Blanks out any phone numbers found in a string. Only phone numbers of the form: ###-#### or ###-###-#### are changed.} text-file [string!] /local blank-number digits match-start match-end ][ blank-number: func [num-start num-end][ change/part num-start "########" num-end ] digits: charset "0123456789" parse/all text-file [ some [ match-start: 1 2 [3 digits "-"] 4 digits match-end: (blank-number match-start match-end) | skip ] ] ] text-file: read %memo.txt blank-phone-numbers text-file print text-file ...and of course nearly always do. (; Apart from the longer "####" (to reduce line-shrinkage) that peforms the same as my two-liner. And someone might just twig that... 1 2 [3 digits "-"] 4 digits is how it finds the phone numbers and be able to change it to catch (say)... ##-#### or ##-##-#### 1 2 [2 digits "-"] 4 digits or... ###-###-###-#### or ###-###-#### or ###-#### 1 3 [3 digits "-"] 4 digits and so on. (Made up phone numbering formats of course.) -- Carl Read