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

Iterating over an hash while removing keys

by Eily (Monsignor)
on Feb 06, 2020 at 16:40 UTC ( [id://11112496]=perlquestion: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    while (my ($k,$v) = first %hash)
    {
      delete $hash{$_} for $k, @$v;
    }
    
  2. or download this
    use strict;
    use warnings;
    use feature 'say';
    ...
      say "Starting from $k -> [ @$v ]";
      rec_delete %hash => $k;
    }
    
  3. or download this
    Input hash:{ 1 => [2 .. 7], 2 => [1], 3 => [1], 4 => [1], 5 => [1], 6 
    +=> [1], 7 => [1], 8 => [9], 9 => [8] }
    
    Try first then each twice in a row to check that the iteration is rest
    +arted
    ...
         Removing 4 -> [ 1 ]
         Removing 6 -> [ 1 ]
         Removing 7 -> [ 1 ]
    
  4. or download this
    While the hash isn't empty
      Take one key (at random) from the hash
        Delete that key and all connected keys
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11112496]
Approved by marto
Front-paged by erzuuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found