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

Parsing attributes in one line using map

by ruoso (Curate)
on Sep 25, 2006 at 10:29 UTC ( #574710=CUFP: print w/replies, xml ) Need Help??

After writing this code, I realized many people may have just not realized this is possible, so I think posting it here would make people think more about it.

I was writing a simple script wich would receive a attr=value,attr2=value2 string and automatically I wrote:

my %attrs = map { split /=/ } split /,/, $attr;

Well, the trick is that map doesn't have to return the same number of elements of the list it receives. If you returns a list inside a map, the resulting list will have more elements than the input list.

This sounds very intuitive to me, but maybe it's not for others...

daniel

Replies are listed 'Best First'.
Re: Parsing attributes in one line using map
by izut (Chaplain) on Sep 25, 2006 at 10:34 UTC
    You can do this (borrowed from 'Perl for System Administrators'):
    my %attrs = split /=|,/, $attr;

    Igor 'izut' Sutton
    your code, your rules.

Re: Parsing attributes in one line using map
by davidrw (Prior) on Sep 25, 2006 at 12:44 UTC
    Returning more (or less even) elements than provided is very natural for map and very commonly used, especially for generating hashes .. here's two relatively trivial examples:
    perl -MData::Dumper -le 'my %cubes = map { $_ => $_**3 } 1..4; print D +umper \%cubes' $VAR1 = { '1' => '1', '2' => '8', '3' => '27', '4' => '64' }; my @users = [ {id=>4324, name=>'blah'}, {id=>1234, name=>'stuff'}, {id +=>5678, name=>'foo'} ]; my %usersByName = map { $_->{id} => $_ } @users; my $id = prompt_for_user_id; my $user = $usersByName{$id};
    Also, see map for more examples (including the basic hash creation), and this quote:
    Evaluates BLOCK or EXPR in list context, so each element of LIST may produce zero, one, or more elements in the returned value.
      Returning more (or less even) elements than provided is very natural for map and very commonly used, especially for generating hashes ..

      For those not familiar with returning less -- use an empty list (). To build on davidrw's example:

      perl -MData::Dumper -le 'my %oddcubes = map { $_%2 ? ($_ => $_**3) : ( +) } 1..9; print Dumper \%oddcubes' $VAR1 = { '1' => 1, '3' => 27, '7' => 343, '9' => 729, '5' => 125 };
Re: Parsing attributes in one line using map
by jdporter (Canon) on Sep 25, 2006 at 14:53 UTC

    split /=/
    should be
    split /=/, $_, 2

    But what if any of the values contains ',' or '='?

    my %attrs = $attr =~ /([^=,]+)=(.*?)(?:(?=,[^=,]+=)|$)/g;
    We're building the house of the future together.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-11-25 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?