home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!uvaarpa!mmdf
- From: dov@menora.weizmann.ac.il (Dov Grobgeld)
- Newsgroups: comp.lang.perl
- Subject: quoteSplit - Split string into array with embedded quotes
- Message-ID: <1992Sep4.085440.9025@uvaarpa.Virginia.EDU>
- Date: 4 Sep 92 08:54:40 GMT
- Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
- Reply-To: dov@menora.weizmann.ac.il
- Organization: The Internet
- Lines: 52
-
- Here is a small useful subroutine I put together. It has probably been
- done before, and it has probably been posted as well. But as has been said
- before here, it is easier to reinvent the wheel in Perl, than to go to the
- junk yard looking for an old one. 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.
-
- Enjoy!
-
-
- #############################################################################
- # quoteSplit. Splits a text string into an array and allows for embedded
- # quoted strings.
- #
- # Examples (perl quoting mechanism q() used to denote input strings...)
- #
- # q(a b c) splits into ('a','b','c')
- # q(ab,c,d) splits into ('ab','c','d')
- # q('a b' c) splits into ('a b', c)
- # q('that\'s', "it ") splits into ("that's", "it")
- #
- # Dov Grobgeld
- # The Weizmann Institute of Science, Israel
- # Email: dov@menora.weizmann.ac.il
- #############################################################################
-
- sub quoteSplit {
- local($_) = $_[0];
- # local(@list);
-
- s/\\"/\01/g; s/\\'/\02/g; # Hide escaped quotation marks
-
- # @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);
- }
-
- while(<>) {
- print join(":", "eSplit($_)),"\n";
- }
-
- --
- ___ ___
- / o \ o \
- Dov Grobgeld ( o o ) o |
- The Weizmann Institute of Science, Israel \ o /o o /
- "Where the tree of wisdom carries oranges" | | | |
- _| |_ _| |_
-
-