in reply to why each/keys/values can't work on slice?
Gives you, of course;my @bb = 1..10; my %bb = @bb; require Data::Dumper; print Data::Dumper::Dumper(\%bb);
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,$VAR1 = { '9' => 10, '5' => 6, '7' => 8, '3' => 4, '1' => 2 };
Takes %bb which was defined directly by @bb, and interacts it as a set of tuples (key/value pairs).while (my ($key, $value) = each %bb) { print qq{$key contains $value\n}; }
And similarly, you may interact with @bb as a HASH using keys and values.3 contains 4 7 contains 8 5 contains 6 9 contains 10 1 contains 2
Which gives us,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; }
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!'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
|
|---|