Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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.";


Comment on Re^2: How to improve regex for parsing equals delimited data
Select or Download Code

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 cooling their heels in the Monastery: (6)
As of 2014-11-23 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (128 votes), past polls