oha has asked for the wisdom of the Perl Monks concerning the following question:
some time ago a monk asked in CB about parsing a CSV-like stream of data without installing any modules.
I played a bit with it as soon as i had some time at the office, and i find out that using the m//g in scalar context a way to do it (it will not fail on wrong data, but that's not the point).
(Note: Not sure if this could help, but i get and extra-match at then end)$_ = "'foo',123,'bar\\'cuz', 'comma,comma',,'void'"; while(/('(.*?)'|([^']*?))($|,\s*)/g) { my $t = $2 || $3; print ">$t\n"; } ___________ >foo >123 >bar\'cuz >comma,comma > >void >
Then i tried doing the same in list content, and i got a weird behaviour; and i was not able to understand why:
Note: I confess i didn't understand correctly how the \G should be used with /g, but i tried using it at the start of the RE and i got the same result.$_ = "'foo',123,'bar\\'cuz', 'comma,comma',,'void'"; map { my $t = $2 || $3; print "<$t\n"; } m/('(.*?)'|([^']*?))($|,\s*)/g; ______________ <'foo' <foo < <, <123 < <123 <, <'bar\'cuz' <bar\'cuz < <, <'comma,comma' <comma,comma < <, < < < <, <'void' <void < < < < < <
Oha
Updated: forgive me, i realized right now i got not the matches, but all the groups. this explain why i get quad the results, but i still not understand how $2 and $3 are passed
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: m//g in list and scalar context differences?
by Anno (Deacon) on Sep 19, 2007 at 11:11 UTC | |
Re: m//g in list and scalar context differences?
by NetWallah (Canon) on Sep 19, 2007 at 20:18 UTC |