Re: How to make a hash out of two arrays,one with keys and other with values?
by ikegami (Patriarch) on Jun 30, 2009 at 05:46 UTC
|
my %hash;
@hash{@Arrayone} = @Arraytwo;
See hash slices in perldata.
You could also do it in one statement with the help of map.
my %hash = map { $Arrayone[$_] => $Arraytwo[$_] } 0..$#Arrayone;
| [reply] [d/l] [select] |
|
|
And, as is often the case, less ops mean more win:
#!/usr/bin/env perl
use Benchmark 'cmpthese';
@k = 1..1000;
@v = 1..1000;
cmpthese(0, {
map => 'my %h1 = map { $k[$_], $v[$_] } 0..$#k',
for => 'my %h2; $h2{$k[$_]} = $v[$_] for 0..$#k',
slice => 'my %h3; @h3{@k} = @v',
});
__END__
Rate map for slice
map 657/s -- -44% -65%
for 1174/s 79% -- -38%
slice 1884/s 187% 60% --
| [reply] [d/l] |
Re: How to make a hash out of two arrays,one with keys and other with values?
by jlk (Hermit) on Jun 30, 2009 at 05:39 UTC
|
Ok, assuming that the arrays will be mapped element for element without mixing and matching, you can do something like this:
use strict;
use warnings;
my @array1 = ('A','B','C','D');
my @array2 = ('1','2','3','4');
my %hash1;
foreach my $key (@array1){
$hash1{$key} = shift(@array2);
}
for my $keys ( sort(keys %hash1 )) {
my $value = $hash1{$keys};
print "$keys => $value\n";
}
Hope this helped.
Regards,
Jeff
"Every time Linux boots, a penguin gets its wings"
| [reply] [d/l] |
|
|
I have this hash and I need to normalise it.But problem is that it is taking the value of first key i.e. '' as minimum value.I want to delete this first key-value pair.How do I go about it?Please help!!
$VAR1 = {
'' => undef,
'D ' => '4.76',
'S ' => '4.50',
'I ' => '3.95',
'T ' => '4.35',
'K ' => '4.36',
'C ' => '4.65',
'P ' => '4.44',
'Y ' => '4.60',
'R ' => '4.38',
'E ' => '4.29',
'V ' => '3.95',
'N ' => '4.75',
'H ' => '4.63',
'G ' => '3.97',
'Q ' => '4.37',
'M ' => '4.52',
'F ' => '4.66',
'L ' => '4.17',
'A ' => '4.35',
'W ' => '4.70'
};
Thanks in advance!!
| [reply] |
Re: How to make a hash out of two arrays,one with keys and other with values?
by dpetrov (Acolyte) on Jun 30, 2009 at 08:59 UTC
|
Or you can use map and shift, like:
map { $hash{ $_ } = shift @array2 } @array1;
| [reply] [d/l] |
Re: How to make a hash out of two arrays,one with keys and other with values?
by hnd (Scribe) on Jun 30, 2009 at 06:29 UTC
|
mapping is the best..... since you have large arrays
use strict;
my %hash=map {@array1[$_]=>@array2[$_]} 0..$#array1;
| [reply] [d/l] |
|
|
my %hash=map {$array1[$_]=>$array2[$_]} 0..$#array1;
(Identical to ikegami's posting.)
Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)
| [reply] [d/l] |
|
|
really sorry.........
i overlooked it.....
| [reply] |
Re: How to make a hash out of two arrays,one with keys and other with values?
by wardmw (Acolyte) on Jun 30, 2009 at 08:16 UTC
|
my @Arrayone = ('A','B','C','D');
my @Arraytwo = ('1','2','3','4');
my (%Arraythree, $ct);
for ($ct=0; $ct <= $#Arrayone; $ct++) {
$Arraythree{$Arrayone[$ct]} = $Arraytwo[$ct];
}
This assumes that keys array and values array match up.
|\/|artin | [reply] [d/l] |
Re: How to make a hash out of two arrays,one with keys and other with values?
by bichonfrise74 (Vicar) on Jul 01, 2009 at 01:49 UTC
|
#!/usr/bin/perl
use strict;
my %hash;
my @one = qw( a b c d );
my @two = qw( 1 2 3 4 );
for (0 .. $#one) {
$hash{$one[$_]} = $two[$_];
}
| [reply] [d/l] |