Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2017-10-20 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (259 votes). Check out past polls.

    Notices?