Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Keep It Simple, Stupid
 
PerlMonks  

Re: Array to hash converstion

by quester (Vicar)
on Jul 15, 2009 at 05:01 UTC ( #780155=note: print w/ replies, xml ) Need Help??


in reply to Array to hash converstion

You want to use the each function. See http://perldoc.perl.org/functions/each.html. (This was the second hit in a Google search for "perldoc print hash each".)


Comment on Re: Array to hash converstion
Re^2: Array to hash converstion
by gem555 (Acolyte) on Jul 15, 2009 at 05:14 UTC
    The above code prints only one pair of values.
    for my $data_pair (@data_list) { print "$data_pair->{'key'},$data_pair->{'value'}\n"; } correct,AAA correction,BBB date,20090303 date,20090308
    These values and keys should be converted to hash map
      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" );
        #!/usr/bin/perl -w use strict; my @value_pairs = qw (one abc two xyz); my %hash = @value_pairs; foreach my $key (keys %hash) { print "key $key is $hash{$key}\n"; } __END__ prints: key one is abc key two is xyz key one is def
        If two keys are same then, output is
        key one is abc key two is xyz
        so for the key one value is def is not printed here. The expected output is
        key one is abc key two is xyz key one is def
        Please tell me how can I get the output like this
        #!/usr/bin/perl -w use strict; use warnings; my @data_list = ( {key => 'correct', value => 'Article_text1'}, {key => 'correct', value => 'Article_text2'}, {key => 'date', value => '2009-01-01'}, {key => 'date', value => '2009-01-02'} ); my @correct; my @date; for my $data_pair (@data_list) { print "Value:$data_pair->{value}:Key:$data_pair->{key}\n"; if($data_pair->{key} eq 'correct'){ @correct = $data_pair->{value}; } if($data_pair->{key} eq 'date'){ @date = $data_pair->{value}; } } print "The array is @correct\n"; print "The array is @date\n";
        The array stores only the last value. How to store all the values into array
Re^2: Array to hash converstion
by davorg (Chancellor) on Jul 15, 2009 at 09:15 UTC
    You want to use the each function.

    I'm not sure I understand how "each" will solve this problem. I don't think that you do either, given that you haven't shown an example.

    See http://perldoc.perl.org/functions/each.html. (This was the second hit in a Google search for "perldoc print hash each".)

    That's nicely self-referential, isn't it? How do you expect someone to come up with a Google search term that includes the name of the function they're looking for?

    --

    See the Copyright notice on my home node.

    Perl training courses

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-04-16 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (422 votes), past polls