Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Cause of an endless loop?

by davidrw (Prior)
on Jun 18, 2005 at 22:15 UTC ( #468036=note: print w/replies, xml ) Need Help??


in reply to Cause of an endless loop?

a second approach to the one above that refactors it a little bit -- hopefully will make it a little clearer on how the structure is traversed.
while( my ($s, $u_arr) = each %test ){ foreach my $u_idx ( 0..$#$u_arr ){ my $u = $u_arr->[$u_idx]; while( my ($n, $value) = each %$u ){ print "$s $u_idx $n: $value - \n"; } } }
And if you don't care about the index, it can be:
while( my ($s, $u_arr) = each %test ){ foreach my $u ( @$u_arr ){ while( my ($n, $value) = each %$u ){ print "$s $n: $value - \n"; } } }
Also, the naming of the temp variables (e.g. $user instead of $u, etc) in this case can make a huge difference in trying to figure out where you are in the complex data structure (or when you revist this code at a later date).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://468036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-10-05 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (24 votes). Check out past polls.

    Notices?