Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I believe this is your question:
"I have an array (@data_list) containing hash references. Each hash reference contains two keys, 'key' and 'value'. I want to turn this into one hash containing 'key'=>'value' pairs. How can I do this?"

My solutions below assume that you have unique key values, or are un-interested in collisions. In the case of a collision, the last reference to the key wins.

Here is a fully verbose version:

my %final_hash; foreach my $data_pair ( @data_list) { $key = $data_pair->{ key }; $value = $data_pair->{value}; $final_hash{ $key } = $value; } use Data::Dumper; print Dumper \%final_hash;

A shorter version:

my %final_hash = map { $_->{key} => $_->{value} } @data_list; #verify the output use Data::Dumper; print Dumper \%final_hash;
And how do we know they work? We test them.

Full version with tests:

#!/usr/bin/perl -w use strict; use Data::Dumper; use Test::More tests => 2; my @data_list = ( { key => 'correct', value => 'AAA' }, { key => 'correction', value => 'BBB' }, { key => 'date', value => '20090303' }, { key => 'date', value => '20090308' }, ); my $expected = { correct => 'AAA', correction => 'BBB', date => '20090303', date => '20090308', }; my %final_hash_long; foreach my $data_pair (@data_list) { my $key = $data_pair->{key}; my $value = $data_pair->{value}; $final_hash_long{$key} = $value; } my %final_hash_short = map { $_->{key} => $_->{value} } @data_list; is_deeply( \%final_hash_long, $expected, "long version works" ); is_deeply( \%final_hash_short, $expected, "short version works" );

In reply to Re^3: Array to hash converstion by spazm
in thread Array to hash converstion by gem555

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found