Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How to improve regex for parsing equals delimited data

by tobyink (Canon)
on May 11, 2012 at 07:12 UTC ( [id://969918]=note: print w/replies, xml ) Need Help??


in reply to How to improve regex for parsing equals delimited data

Personally I'd parse it into a data structure first, and then use that data structure to generate the output:

use Modern::Perl; use String::Trim; use Data::Dumper; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Terse = 1; my @rows; while (<DATA>) { chomp; trim; 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]; } print Dumper \@rows; for (@rows) { say join q(,), map { $_->{value} } @$_; } __DATA__ FIELDA = ONEAL FIELDB = RELAY FIELDC = L1208 FIELDD = ALTS FIELDA = OSSIPEE FIELDB = DISC FIELDC = SOH: 169879251 FIELDD = DISC FIELDA = OSSIPEE FIELDB = RELAY FIELDC = L1201 FIELDD = ALTS FIELDA = OSSIPEE FIELDB = RELAY FIELDC = L1203 FIELDD = ALTS
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: How to improve regex for parsing equals delimited data
by tobyink (Canon) on May 11, 2012 at 07:34 UTC

    Hmmm... I wish that the result of a pre-increment was an lvalue. That would allow this:

    my %x = (field => $F[$_ - 1], value => $F[$_]);

    to become this...

    my %x = (field => $F[(--$_)++], value => $F[$_]);

    or maybe even...

    my %x = (field => $F[--$_++], value => $F[$_]);

    Now that would be a fun idiom! As things stand, this works:

    my %x = (field => $F[--$_], value => $F[++$_]);

    That said, my initial boring version is probably more readable.

    Update: I've just remembered the secret inchworm-on-a-stick operator. This is one of those rare opportunities it's actually useful:

    my %x = (field => $F[~-$_], value => $F[$_]);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: How to improve regex for parsing equals delimited data
by sauoq (Abbot) on May 11, 2012 at 12:36 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://969918]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-18 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found