Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Again, some of you might have seen the question on the CB already. (And again, I came up with a solution just because asking made me see things clear enough). I am wondering about iterating over an hash while removing some of its keys (the current one, and several connected ones). Actually, to explain my problem on the CB I said I'd like to be able to do this:

while (my ($k,$v) = first %hash) { delete $hash{$_} for $k, @$v; }
where first does the same as each, but with a new iterator (ie, from the start). Long story short, this led to this:
use strict; use warnings; use feature 'say'; use Data::Dump qw( pp ); sub first (\%) { my $hash = shift; scalar(keys %$hash); # Force reset each %$hash; } my %hash = ( 1 => [2..7], 8 => [ 9 ], 9 => [ 8 ], (map { $_ => [ 1 ] } 2..7), ); say "Input hash:", pp \%hash; say ""; say "Try first then each twice in a row to check that the iteration is + restarted"; say pp [first %hash], [each %hash] for 1..2; say ""; # Delete a key and all the keys contained in the value array sub rec_delete(\%$); sub rec_delete(\%$) { my ($hash, $key) = @_; my $array = delete $hash->{$key}; return unless $array; say " Removing $key -> [ @$array ]"; rec_delete %$hash => $_ for @$array; } while (my ($k,$v) = first %hash) { say "Starting from $k -> [ @$v ]"; rec_delete %hash => $k; }
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 ([8, [9]], [5, [1]]) ([8, [9]], [5, [1]]) Starting from 8 -> [ 9 ] Removing 8 -> [ 9 ] Removing 9 -> [ 8 ] Starting from 5 -> [ 1 ] Removing 5 -> [ 1 ] Removing 1 -> [ 2 3 4 5 6 7 ] Removing 2 -> [ 1 ] Removing 3 -> [ 1 ] Removing 4 -> [ 1 ] Removing 6 -> [ 1 ] Removing 7 -> [ 1 ]
This works just fine. But I'm curious about other ways to do the same (because this seems like it could be a normal enough use of hashes?). So the exercise is:
While the hash isn't empty Take one key (at random) from the hash Delete that key and all connected keys
Without expanding the whole list of keys (let's pretend there's an awful lot of them, or the hash is tied, and it's harder to find an existing key than fetch a value). So @keys = keys %hash; for my $key (@keys) { recursive_delete(\%hash, $key); } isn't valid.

NB: the output of my example is actually random, so you might get a different cycle by trying it.


In reply to Iterating over an hash while removing keys by Eily

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-03-19 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found