This works without "hanging". I think that foreach (keys %$hash){} builds a list that is then iterated over and therefore what DumpFile does with the internal hash iterator doesn't matter. Of course in any event, DumpFile should be moved outside of the loop - no need to create it over and over again.#!/usr/bin/perl use strict; use warnings; use YAML::Syck; my $hash = { 'k1'=>'v1', 'k2'=>'v2', 'k3'=>'v3', }; # Part 1 - works fine while ( my ($k, $v) = each(%$hash) ) { print "$k, $v\n"; } # Part 3 - use foreach loop instead of while(each) foreach my $k (keys %$hash) { print "$k, $hash->{$k}\n"; DumpFile("test.yml", $hash); } __END__ stdout prints: k3, v3 k2, v2 k1, v1 k3, v3 k2, v2 k1, v1 test.yml file has: --- k1: v1 k2: v2 k3: v3
In reply to Re: Action on each key/value pair in a hash
by Marshall
in thread Action on each key/value pair in a hash
by luxs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |