Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

array to hash

by sunil9009 (Acolyte)
on Oct 20, 2014 at 11:06 UTC ( [id://1104416]=perlquestion: print w/replies, xml ) Need Help??

sunil9009 has asked for the wisdom of the Perl Monks concerning the following question:

How can I convert:
my $items = [{'a' => 1},{'b' => 2},{'c' => 3}];
to
my $values = {{'a' => 1},{'b' => 2},{'c' => 3}};
tried something like my $values = map {$_ => $_->{$_}} keys $_ map $_, @{$items}; and gave up

Replies are listed 'Best First'.
Re: array to hash
by McA (Priest) on Oct 20, 2014 at 11:09 UTC

    Hi,

    you need always a key-value-pair for a hash. So, what is the key in

    {{'a' => 1},{'b' => 2},{'c' => 3}};

    UPDATE: Or do you mean:

    {'a' => 1,'b' => 2,'c' => 3};

    UPDATE2: In that case it could be something like:

    my $items = [{'a' => 1}, {'b' => 2}, {'c' => 3}]; my $values = {map { %$_ } @$items};

    Regards
    McA

Re: array to hash (law of dictionary hashes)
by Anonymous Monk on Oct 20, 2014 at 11:11 UTC

    The law of hashes , the law of dictionary , states, keys are words, keys are strings, keys cannot be reference, hashes or arrays, keys are only strings, they're key words, like in a dictionary, a hash is a dictionary

    so you cannot write my %hash = ( \%foo => \%bar ) as \%foo becomes a string ...

    so whatever you had in mind for $values, you need to type up a different example of what you want, an example that is more sensible:)

    unless you really wanted the stringification of \%foo as an actual key (I doubt that)

Re: array to hash
by sunil9009 (Acolyte) on Oct 20, 2014 at 17:43 UTC
    Thank you everyone, that solved my issue.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found