home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5732 < prev    next >
Encoding:
Internet Message Format  |  1992-09-07  |  1.6 KB

  1. Path: sparky!uunet!ogicse!uwm.edu!zaphod.mps.ohio-state.edu!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: quoteSplit - Split string into array with embedded quotes
  5. Message-ID: <1992Sep4.170954.2559@netlabs.com>
  6. Date: 4 Sep 92 17:09:54 GMT
  7. Article-I.D.: netlabs.1992Sep4.170954.2559
  8. References: <1992Sep4.085440.9025@uvaarpa.Virginia.EDU>
  9. Sender: news@netlabs.com
  10. Organization: NetLabs, Inc.
  11. Lines: 42
  12. Nntp-Posting-Host: scalpel.netlabs.com
  13.  
  14. In article <1992Sep4.085440.9025@uvaarpa.Virginia.EDU> dov@menora.weizmann.ac.il writes:
  15. : There is a nice use of m//g in the
  16. : script, and a blasting one-liner, which to the uninitiated certainly
  17. : would look like line-noise.
  18. : #    @list= /"[^"]*"|'[^']*'|[^\s,]+/g;
  19. : #    grep(s/^(["'])(.*)\1$/$2/ + tr/\1\2/"'/, @list);
  20. : #    @list;
  21. :     # This one liner is equivalent to the the three lines above
  22. :     grep(1+s/^(["'])(.*)\1$/$2/+tr/\01\02/"'/,/"[^"]*"|'[^']*'|[^\s,]+/g);
  23.  
  24. Much as I enjoy a good one-liner,
  25.  
  26.     grep(
  27.     1 + s/^(["'])(.*)\1$/$2/ + tr/\01\02/"'/,
  28.     /"[^"]*"|'[^']*'|[^\s,]+/g
  29.     );
  30.  
  31. is much more readable and does the same thing.  If you want to get fancy
  32. and reduce the slash count, you can say (in 4.035):
  33.  
  34.     grep(
  35.     1 + s<^(["'])(.*)\1$><$2> + tr[\01\02]["'],
  36.     /"[^"]*"|'[^']*'|[^\s,]+/g
  37.     );
  38.  
  39. It might or might not be a hair faster to say
  40.  
  41.     grep(
  42.     (
  43.         s<^(["'])(.*)\1$><$2>,
  44.         tr[\01\02\03-\377]["'\03-\377]
  45.     ),
  46.     /"[^"]*"|'[^']*'|[^\s,]+/g
  47.     );
  48.  
  49. Interesting though that there's this deep etymological relationship
  50. between various forms of implicit looping and the letter g...
  51.  
  52. Larry
  53.