Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Reset while loop to beginning of hash when using 'each'

by seaver (Pilgrim)
on Sep 25, 2003 at 23:09 UTC ( [id://294285]=perlquestion: print w/replies, xml ) Need Help??

seaver has asked for the wisdom of the Perl Monks concerning the following question:

Dear all, I am looping through the keys of a hash like so:
while( my ($res, $ats) = each %{$self->{'residues'}{$modelCount}} ) { if($ats eq $line){ do stuff to $line; last LINE; } }
LINE refers to the while loop that is looping through the file, and each $line of the file, if it meets the requirements, is used.

However, the inner while loop shown above does not reset itself, consequently, the next $line doesnt have stuff happening to it, because no $ats matches it, THEN, because the while loop reaches the end of the hash, it goes back and does it again, but to the NEXT line in the file.

The result of which seems like this loop is skipping every other $ats

I'm trying to find information on resetting while loops, but, though I've found something on making the $_ local, it doesnt seem to apply here?

Anyone know what to do?

Cheers
Sam

Replies are listed 'Best First'.
Re: Reset while loop to beginning of hash when using 'each'
by davido (Cardinal) on Sep 25, 2003 at 23:23 UTC
    According to the perldoc for each you can reset the each iterator function in one of the following ways:

    • Iterate over the entire hash. Upon final iteration, zero or null is returned depending on context, and the next call to each will start over again. You might find this to be slightly inconvenient if the point is to break out of a loop before iterating over all elements.
    • evaluate keys %hash.
    • evaluate values %hash.

    You might think that evaluating keys for the hash is an inefficient way of resetting the each iterator. It might be, if you were to evaluate keys in list context. Don't. Just evaluate keys in scalar context, which efficiently returns the number of keys, rather than the list of keys. After that, you'll find each has been reset as desired.

    Here's a sample illustration:

    use strict; use warnings; my %hash = ( Who => 'Me', What => 'Person', How => 'Conception', When => '35 years ago', Where => 'I dont want to know', Why => 'Irrelevant' ); while ( my ( $key, $value ) = each %hash ) { print "$key => $value\n"; scalar keys %hash; }

    If you run that you'll now find yourself in a neverending loop, since each gets reset each time through the while loop. In this illustration, the use of the pseudo-function scalar is unnecessary. As long as keys isn't being evaluated in list context, Perl is smart enough to not go to the work of creating a list of keys that just gets thrown away.

    Hope this helps...

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      Fantastic, it hadnt occured to me that the iteration was in the 'each' function. Great answer too, so easy to read!

      Cheers
      Sam

Re: Reset while loop to beginning of hash when using 'each'
by Nkuvu (Priest) on Sep 25, 2003 at 23:25 UTC
    The Camel says (in the definition of the each function):
    There is a single iterator for each hash, shared by all each, keys and values function calls in the program; it can be reset by reading all the elements from the hash, or by evaluating keys %hash or values %hash.
Re: Reset while loop to beginning of hash when using 'each'
by BrowserUk (Patriarch) on Sep 25, 2003 at 23:25 UTC

    From perlfunc:each

    here is a single iterator for each hash, shared by all each, keys, and values function calls in the program; it can be reset by reading all the elements from the hash, or by evaluating keys HASH or values HASH

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Reset while loop to beginning of hash when using 'each'
by perlmonkey (Hermit) on Sep 25, 2003 at 23:31 UTC
    according to the each perldoc page, you have to reset the each call or else it will always keep iterating from where you left off.

    To reset the is says execute: keys %hash; so you need something like:
    # reset each keys %{$self->{'residues'}{$modelCount}}; while( my ($res, $ats) = each %{$self->{'residues'}{$modelCount}} ) { if($ats eq $line){ do stuff to $line; last LINE; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://294285]
Approved by HyperZonk
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found