Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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 wandering the Monastery: (15)
As of 2015-07-07 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls