Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Why does each() always re-evaluate its argument? ("for_list" )

by LanX (Saint)
on Dec 07, 2023 at 02:49 UTC ( [id://11156154]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Why does each() always re-evaluate its argument? (Updated x2 - experimental "for_list" )
in thread Why does each() always re-evaluate its argument?

> 1) There's no copying,

If that's so, then the docs should be clearer:

> 2) it uses the built-in iterator.

In that case, I suppose nested loops over the same hash are covered by saving and restoring the localized iterator?

I was expecting a more intelligent solution than literally "copying to a flat list" but this also leads to another question:

What happens if the hash is altered inside the loop, if it's not copied "before the loop starts"

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

  • Comment on Re^5: Why does each() always re-evaluate its argument? ("for_list" )

Replies are listed 'Best First'.
Re^6: Why does each() always re-evaluate its argument? ("for_list" )
by ikegami (Patriarch) on Dec 07, 2023 at 20:11 UTC

    In that case, I suppose nested loops over the same hash are covered by saving and restoring the localized iterator?

    Nope, no need. Like you quoted, the hash's contents are fetched up front.

      > fetched up front.

      I'm confused, you just explained

      > > 1) There's no copying, 2) it uses the built-in iterator.

      Did you correct yourself or am I misunderstanding you?

      FWIW: I'm installing 5.38.2 for testing via Perlbrew and it takes hours...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        It uses the built-in iterator to obtain the keys and values from the hash and push them unto the stack before any looping is performed.

        If you want me to be more precise, no extra copying occurs.

        • The keys are copied (i.e. fresh scalars are made for the keys) since they're not found as scalars in the hash. But that has to happen no matter the interface.
        • The pointers to the scalars placed on the stack. But that has to happen no matter the interface.
        $ perl -e' use v5.14; use experimental qw( for_list ); my %h = ( a => 4, b => 6 ); my $i = 0; for my ( $k, $v ) ( %h ) { if ( !$i++ ) { ++$h{a}; ++$h{b}; } say "$k:$v"; } ' a:5 b:7

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found