Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: strange map behavior

by ikegami (Patriarch)
on Feb 25, 2008 at 17:27 UTC ( [id://670055]=note: print w/replies, xml ) Need Help??


in reply to strange map behavior

Perl sometimes guesses a block is an anonymous hash constructor and vice-versa.
my %h = map {; $_ => $_ } @arr; ^ | force a block.
my @a = map +{ $_ => ord($_) }, @arr; ^ | force a hash

By the way, why are you using "$_" instead of just $_?

Replies are listed 'Best First'.
Re^2: strange map behavior
by jeanluca (Deacon) on Feb 25, 2008 at 19:24 UTC
    I tried to build a new hash out of an array with hashes, something like:
    #! /usr/bin/perl -l use strict; use warnings ; my @arr = ( {'a' => 'x', 'b' => 'y', 'c' => 'z'}, {'a' => 'x1', 'b' => 'y1', 'c' => 'z1'} ) ; my %h = map {;"$_->{'a'}_$_->{'c'}" => $_->{'b'} } @arr ; print "$_ -- $h{$_}" foreach ( keys %h ) ;
    Thnx
    LuCa
      That makes more sense. But you might want to use join to increase readability.
      my %h = map { join('_', $_->{'a'}, $_->{'c'}) => $_->{'b'} } @arr ;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://670055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-25 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found