When you define my @bb = 1..10;, you're clearly creating an ARRAY. However, this has some relevance to a HASH. For example,
my @bb = 1..10;
my %bb = @bb;
require Data::Dumper;
print Data::Dumper::Dumper(\%bb);
Gives you, of course;
$VAR1 = {
'9' => 10,
'5' => 6,
'7' => 8,
'3' => 4,
'1' => 2
};
Where the odd numbers are the keys and the even numbers are the values. You mention each, so this will work as expected if you're treating @bb as a HASH. For example,
while (my ($key, $value) = each %bb) {
print qq{$key contains $value\n};
}
Takes %bb which was defined directly by @bb, and interacts it as a set of tuples (key/value pairs).
3 contains 4
7 contains 8
5 contains 6
9 contains 10
1 contains 2
And similarly, you may interact with @bb as a HASH using keys and values.
printf qq{\n'keys' over "%s"\n}, join q{, }, @bb;
foreach my $key (keys %{ {@bb} }) {
printf qq{$key contains %s\n}, $bb{$key};
}
printf qq{\n'values' over "%s"\n}, join q{, }, @bb;
foreach my $value (values %{ {@bb} }) {
printf qq{key contains %s\n}, $value;
}
Which gives us,
'keys' over "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
7 contains 8
9 contains 10
1 contains 2
5 contains 6
3 contains 4
'values' over "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
key contains 6
key contains 10
key contains 2
key contains 4
key contains 8
Some time ago when I realized that hashes were really just a list of pairs, a lightbulb went off for me. I think that a similar realization will benefit you. There are things I don't know without playing with them, and I don't have the time at the moment; but for example, how does each interact with %{ { @bb } } - and is there a better way to pass an ARRAY in a HASH context? I don't know, will keep my eyes on this thread to see if anyone else has some more to say about it. Good luck!
|