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!

In reply to Re: why each/keys/values can't work on slice? by perlsherpa
in thread why each/keys/values can't work on slice? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.