Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:
So far I have something like this:
$string = 'one two "three" four "five" six'; $count_quotes = $string =~ tr/"/"/; if($count_quotes && ($count_quotes % 2 != 0)){ # there is an odd number of quotes in the string $quotedstringerror = 1; # use this to show user an error message later }elsif($count_quotes){ # there is an even number of quotes in the string $quotedstrings = 1; } if($quotedstrings){ @quotedstrings = $string =~ /"([^"]*?)"/g; # get them all out with m// $string =~ s/"[^"]*?"//g; # remove them from the original with s/// } @unquotedstrings = split(/ +/,$string); # grab remaining regular strings using split print "\@quotedstrings: @quotedstrings \n"; print "\@unquotedstrings: @unquotedstrings \n"; # output: @quotedstrings: three five # @unquotedstrings: one two four six
This seems to work OK, but could I be doing it in a better way?
Do I really have to use tr/// to count them (because m// doesn't return a number of matches), then use m// to get them out (because s/// doesn't push finds onto an array), then use s/// to clean up after myself?
I keep thinking there's something I've missed.
--
Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.
M-J D
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: separated quoted stings from unquoted strings
by steves (Curate) on Feb 15, 2003 at 10:36 UTC | |
|
Re: separated quoted stings from unquoted strings
by Enlil (Parson) on Feb 15, 2003 at 10:13 UTC | |
|
Re: separated quoted stings from unquoted strings
by Coruscate (Sexton) on Feb 15, 2003 at 10:00 UTC | |
|
Re: separated quoted stings from unquoted strings
by BrowserUk (Patriarch) on Feb 15, 2003 at 05:28 UTC | |
|
Re: separated quoted stings from unquoted strings
by Dr. Mu (Hermit) on Feb 16, 2003 at 00:00 UTC | |
|
Re: separated quoted stings from unquoted strings
by hv (Prior) on Feb 17, 2003 at 11:27 UTC |