home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!uwm.edu!zaphod.mps.ohio-state.edu!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: quoteSplit - Split string into array with embedded quotes
- Message-ID: <1992Sep4.170954.2559@netlabs.com>
- Date: 4 Sep 92 17:09:54 GMT
- Article-I.D.: netlabs.1992Sep4.170954.2559
- References: <1992Sep4.085440.9025@uvaarpa.Virginia.EDU>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 42
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <1992Sep4.085440.9025@uvaarpa.Virginia.EDU> dov@menora.weizmann.ac.il writes:
- : There is a nice use of m//g in the
- : script, and a blasting one-liner, which to the uninitiated certainly
- : would look like line-noise.
- :
- : # @list= /"[^"]*"|'[^']*'|[^\s,]+/g;
- : # grep(s/^(["'])(.*)\1$/$2/ + tr/\1\2/"'/, @list);
- : # @list;
- :
- : # This one liner is equivalent to the the three lines above
- : grep(1+s/^(["'])(.*)\1$/$2/+tr/\01\02/"'/,/"[^"]*"|'[^']*'|[^\s,]+/g);
-
- Much as I enjoy a good one-liner,
-
- grep(
- 1 + s/^(["'])(.*)\1$/$2/ + tr/\01\02/"'/,
- /"[^"]*"|'[^']*'|[^\s,]+/g
- );
-
- is much more readable and does the same thing. If you want to get fancy
- and reduce the slash count, you can say (in 4.035):
-
- grep(
- 1 + s<^(["'])(.*)\1$><$2> + tr[\01\02]["'],
- /"[^"]*"|'[^']*'|[^\s,]+/g
- );
-
- It might or might not be a hair faster to say
-
- grep(
- (
- s<^(["'])(.*)\1$><$2>,
- tr[\01\02\03-\377]["'\03-\377]
- ),
- /"[^"]*"|'[^']*'|[^\s,]+/g
- );
-
- Interesting though that there's this deep etymological relationship
- between various forms of implicit looping and the letter g...
-
- Larry
-