corenth has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to parse a simple string. In doing so, I want to get rid of quotes. The regex as it stands doesn't reflect the possibility of escaped quotes.
Let's take this string: '1,2,3,4,"fine","day","today",'
If I could remove all the qoutes (or didn't care) I could use:to give me :@a = $string =~/ ( #capture matches and put them into @a (?:\d+) | (?:"(?:.+?)") ) /gx; print Dumper \@a;
I don't wan't those quotes, so I :$VAR1 = [ '1', '2', '3', '4', '"fine"', '"day"', '"today"' ];
which gives me:@b = $string =~/ (?: #don't capture matches (\d+) | (?:"(.+?)") #capture numbers and anything inside of quotes ) /gx; print Dumper \@b;
Oh NO!!! all those undefs and I might want to use a hash instead of an array!! Oh, what can I do?$VAR1 = [ '1', undef, '2', undef, '3', undef, '4', undef, undef, 'fine', undef, 'day', undef, 'today' ];
to get:@c = map {/.+/g} #if it's an undef, it's NOT getting into that array ;) $string =~/ (?: (\d+) | (?:"(.+?)") ) /gx; print Dumper \@c;
LOVELY!! But why? Granted, this exercise has helped me to understand map() alot better, but I would like to know what it is about my pattern that creates all those undefs, and I would like to know how (if there's a way) I could do this without all those undefs. I'm happy with my solution, but I'm unhappy with the fact that I don't understand the problem. If you have an idea about how to help me understand, I'd be very thankful.$VAR1 = [ '1', '2', '3', '4', 'fine', 'day', 'today' ];
---
($state{tired})?(sleep($wink * 40)): (eat($food));
Back to
Seekers of Perl Wisdom