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
In reply to separated quoted stings from unquoted strings by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |