in reply to why each/keys/values can't work on slice?

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

Replies are listed 'Best First'.
Re^2: why each/keys/values can't work on slice? (updated)
by Anonymous Monk on Jan 11, 2023 at 03:10 UTC
    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

        use feature qw/say/; my @bb = 1..10; while(my ($ind, $val) = each @bb){ say "ind is $ind"; if($ind >5){ while(my ($inner_ind, $inner_val) = each @bb){ say "inner ind is $inner_ind, inner val is $inner_v +al" } } } # will endless loop since while share one iterator while(my ($ind, $val) = each @bb){ say "ind is $ind"; } # this will quit loop successfully.
        I don't understand why every array has to have only one iterator, please enlighten me. and guess each/keys doesn't work on slice would be relative to this reason?