Perl-Sensitive Sunglasses PerlMonks

### Populating a Hash: Can someone help me to understand?

by Limo (Scribe)
 on Sep 19, 2000 at 19:03 UTC Need Help??

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

What I'd like to do is create:
```my %hash;
where:
```@keys = hash keys
@values = key values
I've read about references from several sources; I think referencing the arrays are my answer, but I'm just not getting it.

Replies are listed 'Best First'.
Re: Populating a Hash: Can someone help me to understand?
by japhy (Canon) on Sep 19, 2000 at 19:10 UTC
@hash{@keys} = @values;

\$_="goto+F.print+chop;\n=yhpaj";F1:eval
Re: Populating a Hash: Can someone help me to understand?
by ar0n (Priest) on Sep 19, 2000 at 19:08 UTC
(ugly stuff removed, replaced with less ugly)
```map { \$hash{\$keys[\$_]} = \$vals[\$_]  } (0..\$#keys);

update
but there are nicer, prettier ways to do this; as you'll probably see below...
You said:
```while (my \$key = shift @keys and my \$value = shift @values) {
\$hash{\$key} = \$value;
}
And your code stops too quickly if either the key or the value is false.
You updated:
```while (defined (my \$key = shift @keys) and defined(my \$value = shift @
+values)) {
\$hash{\$key} = \$value;
}
But even adding the defined test as you did doesn't help. Consider @keys = (1, 2, undef, 3, 4);. You'll never get to 3 or 4.

What would work is:

```while (@keys and @values) {
\$hash{shift @keys} = shift @values;
}
Hmmm. Something strange here.
```my @array = subroutine(\$file1, \$list1);
#foreach my \$item(@array) {
#print "\$item\n";
# }
Now, when I uncomment the foreach/print block, it prints as intended.
```   my %hash;
while (@keys and @values) {
\$hash{shift @keys} = shift @values;
}

foreach my \$k (sort keys %hash) {
print "\$k => \$hash{\$k}\n";

}
the program returns only one value per key, while @values contains several whitespace delimited elements.
RE: Populating a Hash: Can someone help me to understand?
by Adam (Vicar) on Sep 19, 2000 at 19:44 UTC
Japhy's got it in Re: Populating a Hash: Can someone help me to understand?, but I figured you might want the reverse too:
```my @values = values %hash;
my @keys = keys %hash;
Not that I would do that (since I like to keep the data in one place, and values and keys are such nice hooks. But I tell you this in case you didn't know about them.
(Guildenstern: Hash Slice?) RE: Populating a Hash: Can someone help me to understand?
by Guildenstern (Deacon) on Sep 19, 2000 at 20:14 UTC
Why can't you just use a hash slice? Like this:@hash{@keys} = @values;I imagine this would require that both arrays have equal numbers of values, but then any other kind of hash population would likely assume that as well.
Update: Okay, so I missed the post by japhy. At least merlyn had the common courtesy to explain why he --'d it.

Guildenstern
Negaterd character class uber alles!

Create A New User
Node Status?
node history
Node Type: perlquestion [id://33098]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
As of 2020-04-07 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The most amusing oxymoron is:

Results (42 votes). Check out past polls.

Notices?