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

Hi monks

slice is of course a powerful expression in perl, which many other languages borrow it. but why each/keys/values can't work on it? is difficult to implement? Please enlighten me.

C:\Users\Administrator>perl -E"my @bb = 1..10; for(keys @bb[1..5]){say +;} Experimental keys on scalar is now forbidden at -e line 1. Type of arg 1 to keys must be hash or array (not array slice) at -e li +ne 1, near "])" Execution of -e aborted due to compilation errors. C:\Users\Administrator>perl -E"my @bb = 1..10; @cc = @bb[1..5]; for(ke +ys @cc){say;} 0 1 2 3 4

Replies are listed 'Best First'.
Re: why each/keys/values can't work on slice?
by ikegami (Patriarch) on Jan 10, 2023 at 15:35 UTC

    Why would you want to use keys @bb[1..5] when you can use 1..5?

    Why would you want to use values @bb[1..5] when you can use @bb[1..5]?

      well,that’s just a example, slice could contains single elements and range even like @bb1,3,-1 etc.IMHO, slice is an accessor of array. So I am really curious why Perl emphasize keys,each only work on array and hash except for slice.

        Then you could use 1, 3, -1 instead of keys @bb[ 1, 3, -1 ].

        Generally speaking,

        You could use LIST instead of keys @bb[ LIST ].

        You could use @bb[ LIST ] instead of values @bb[ LIST ].

Re: why each/keys/values can't work on slice? (updated)
by LanX (Saint) on Jan 10, 2023 at 14:58 UTC
    ehm keys @bb[1..5] should return 1..5 (if defined) not 0..4 like demonstrated by you.

    This ambiguity in interpretation should already explain why it's not implemented.

    update

    the technical answer is that each/keys/values operate on variables like hashes (or arrays in newer Perls) and manipulate an internal iterator counter.

    But a slice returns a LIST and not a variable, otherwise your intended feature would require implementing separate counters for each slice.

    Anyway you can always create a temporary variable on the fly, if you really need to apply each/keys/values ...

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      That means in perl, every array/hash has only one own iterator. Why perl has this limitation?
        > Why perl has this limitation?

        Well ... maybe show us an example from another language w/o "this limitation" to learn from? :)

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: why each/keys/values can't work on slice?
by karlgoethebier (Abbot) on Jan 11, 2023 at 15:46 UTC
    «…slice…many other languages borrow it.»

    It is always more complicated. It all started in 1966.

    «The Crux of the Biscuit is the Apostrophe»

Re: why each/keys/values can't work on slice?
by perlsherpa (Novice) on Jan 12, 2023 at 04:31 UTC
    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!