SPUG: each() on hashref giving INFINITE LOOP!
Tim Maher/CONSULTIX
tim at consultix-inc.com
Fri Jan 21 17:32:45 CST 2000
The following program, a simple demo for passing and using a hash-ref, is
getting an infinite loop when using while(each) in the sub, but not
when using foreach there! Can anybody shed some light on this?
-Tim
$ animal2title
Here is the hash: Camel Programming Perl Llama Learning Perl
These are the keys: Camel Llama
Camel -> Programming Perl
Llama -> Learning Perl
(while loop starts now)
Camel -> Programming Perl
Camel -> Programming Perl
Camel -> Programming Perl
Camel -> Programming Perl
Infinite Loop Terminated!
#! /usr/bin/perl -w
# animal2title
use strict;
#sub print_hash; # not complaining this time, for some reason!
$main::DEBUG=1;
my $h_ref = { } ; # create reference to empty hash
# Load animals and their corresponding O'Reilly Perl-book titles
$h_ref->{Camel} = 'Programming Perl';
$h_ref->{Llama} = 'Learning Perl';
if ($main::DEBUG) {local $,=' '; print "Here is the hash: ", %$h_ref, "\n"; }
print_hash ( $h_ref ); # call sub that takes hash ref
sub print_hash {
my ($i, $key, $value, $hashref);
if (ref ($hashref=shift) ne 'HASH') {
warn ((caller 0)[3], # provides sub's name
"(): argument \"$hashref\" not hash ref\n");
return 0;
}
if ($main::DEBUG) {
local $,=' ';
print "These are the keys: ", keys %$hashref, "\n";
}
foreach ( keys %$hashref ) {
print "$_ -> $$hashref{$_}\n";
}
print "\n";
$i=0; # sense and break out of infinite loop!
while ( ($key,$value) = %$hashref ) {
print "$key -> $value\n";
$i++; # break out of infinite loop
$i > 3 and die "\tInfinite Loop Terminated!\n";
}
return 1;
}
TIA, as usual,
*========================================================================*
| Tim Maher, PhD Consultix & (206) 781-UNIX/8649 |
| Pacific Software Gurus, Inc Email: tim at consultix-inc.com |
| UNIX/Linux & Perl Training http://www.consultix-inc.com |
| 2/22: UNIX 2/28: Perl Modules 2/29: Int. Perl 3/3: Pattern Matching |
*========================================================================*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
Email to majordomo at pm.org: ACTION spug-list your_address
More information about the spug-list
mailing list