SPUG: each() on hashref giving INFINITE LOOP!
Bradley E. Young
byoung at speakeasy.org
Fri Jan 21 19:36:05 CST 2000
Tim,
Looks like the last chunk is giving you fits because it doesn't iterate over
the hash. You are setting $key and $value to the first two elements of
%$hash every loop through.
I think that you might have meant to do this:
$i=0; # sense and break out of infinite loop!
while ( ($key,$value) = each %$hashref ) {
print "$key -> $value\n";
$i++; # break out of infinite loop
$i > 3 and die "\tInfinite Loop Terminated!\n";
}
return 1;
Notice the addition of each().
Brad
> -----Original Message-----
> From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org]On Behalf Of
> Tim Maher/CONSULTIX
> Sent: Friday, January 21, 2000 3:33 PM
> To: spug-list at pm.org
> Subject: SPUG: each() on hashref giving INFINITE LOOP!
>
>
> 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
>
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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