Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: How to improve regex for parsing equals delimited data

by sauoq (Abbot)
on May 11, 2012 at 12:36 UTC( #969997=note: print w/replies, xml ) Need Help??


in reply to Re: How to improve regex for parsing equals delimited data
in thread How to improve regex for parsing equals delimited data

my @F = split /\s*=\s*/; push @rows, [map { my %x = (field => $F[$_ - 1], value => $F[$_]); $x{field} =~ s/.*\s+(\S+)$/$1/ unless $_ == 1; $x{value} =~ s/\s*\S+$// unless $_ == $#F; \%x; } 1 .. $#F];

You could improve that a great deal if you just used one of the splits already given with the slight modification of capturing the field name. There's no need for all the conditionals, substitutions, array indexing, and length checking. It's more readable too. . .

my ($toss, @list) = split /\s*([A-Za-z]+)\s*\=\s*/; my @row; while (@list) { push @row, { field => shift @list, value => shift @list }; } push @rows, \@row;

-sauoq
"My two cents aren't worth a dime.";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2016-08-30 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (410 votes). Check out past polls.