Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Iterating over an hash while removing keys

by choroba (Cardinal)
on Feb 06, 2020 at 16:55 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112500]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-19 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found