Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Iterating over an hash while removing keys

by choroba (Archbishop)
on Feb 06, 2020 at 16:55 UTC ( #11112500=note: print w/replies, xml ) Need Help??


in reply to Iterating over an hash while removing keys

You don't have to call scalar. Using a void context resets the iterator without even checking the keys at all.
- scalar(keys %$hash); # Force reset + keys %$hash; # Force reset

Update: Documented in keys:

> In particular, calling "keys" in void context resets the iterator with no other overhead.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Iterating over an hash while removing keys
by Eily (Monsignor) on Feb 06, 2020 at 16:57 UTC

    That was actually my first version, and I forgot to ask about the behaviour of keys in void context (it was really likely that it didn't expand the list of keys needlessly, but better safe than sorry :) ).

    Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2020-05-29 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (170 votes). Check out past polls.

    Notices?