Re: Array to hash converstion
by NetWallah (Canon) on Jul 15, 2009 at 05:02 UTC
|
What you seem to have is an array-of-hash-refs (AKA AoH).
If each hashref contains only a single key/value pair, you can do this:
my %newhash;
for my $href (@data_list){
$newhash{ $href->{key} } = $href->{value};
}
## If you want to print the new hash ...
for my $k (sort keys %newhash){
print "$k => $newhash{$k}\n";
}
*Untested*
Update: You could also use map:
my %newhash = map {{ $_->{key} } = >$_->{value} } @data_list;
Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
my %newhash = map {{ $_->{key} } = >$_->{value} } @data_list;
>perl -wMstrict -MData::Dumper -le
"my @d_pairs = (
{ key => 'foo', value => 'bar' },
{ key => 'fee', value => 'fie' },
);
my %hash = map { $_->{key} => $_->{value} } @d_pairs;
print Dumper \%hash;
"
$VAR1 = {
'fee' => 'fie',
'foo' => 'bar'
};
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Array to hash converstion
by ELISHEVA (Prior) on Jul 15, 2009 at 06:19 UTC
|
my @data_list = ( { key => 'a', value => 1 }
, { key => 'b', value => 2 }
, { key => 'c', value => 3 }
);
#this line does the conversion
my %hData = map { $_->{key} => $_->{value} } @data_list;
print "@{[%hData]}\n";
For more information, please see map, perldsc, and perldata.
Best, beth | [reply] [Watch: Dir/Any] [d/l] |
Re: Array to hash converstion
by Marshall (Canon) on Jul 15, 2009 at 05:21 UTC
|
Converting @data_list to a hash is easy. See below code. Basically if you assign an array to a hash, Perl treats this as key, value pairs. my %hash = @data_list; is all that is needed.
#!/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
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
Yes, I did engage in a bit of "mind-reading" here. I didn't see any example input data or any example output data. I wound up not assuming that this was working code and I took a guess as to intent based upon the context of the question. If the OP is comfortable with AOH and wants HOH, then 1)So what are the keys to this HOH supposed to be? I didn't see anything about that. 2)Also, if the OP really is comfortable working with AoH, then AoH to HoH isn't that big of a leap and I would have expected some kind of "this is my attempt" code.
I try to helpful. But, in general the usefulness and quality of the responses depends a LOT on the clarity of the question.
| [reply] [Watch: Dir/Any] |
Re: Array to hash converstion
by quester (Vicar) on Jul 15, 2009 at 05:01 UTC
|
| [reply] [Watch: Dir/Any] |
|
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?
| [reply] [Watch: Dir/Any] |
|
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 | [reply] [Watch: Dir/Any] [d/l] |
|
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" );
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
|