Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: why each/keys/values can't work on slice? (updated)

by LanX (Saint)
on Jan 10, 2023 at 14:58 UTC ( [id://11149494]=note: print w/replies, xml ) Need Help??


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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149494]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found