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

I'm having mental block today... forgive me. I had a workaround to this once, but can't remember what I did (I know -- terrible).

I've tied a file. I go through the file, and find the line I want. Problem is -- the timestamps are on other lines. Also, it's not a specific # of lines away from the line that has my info so simply adding X to the index and grabbing the timestamp won't work.

So, I basically need to go through each line in the array. When my condition returns true, I need to then start ANOTHER loop, and go until I find a timestamp. Now, the kicker... how do I exit this second loop once I hit a timestamp?

I can always let it run through like the next 500 lines (it would surely find one there) and then only take the first timestamp I find, but that's horribly inefficient me thinks. Thanks

Replies are listed 'Best First'.
Re: Exiting a loop trickery
by florg (Friar) on Mar 11, 2004 at 01:48 UTC

    I think you're looking for last:

    #!/usr/bin/perl -w use strict; while (<DATA>) { next unless /trigger/; while (<DATA>) { if ( $_ =~ /timestamp/ ) { print "Found timestamp\n"; last; } } } __DATA__ bar baz trigger qux timestamp quux quuux trigger quuuux timestamp bar
    florg
      Or, to avoid nesting loops, use a range operator:
      while (<DATA>) { if (/trigger/ .. /timestamp/) { if ( $_ =~ /timestamp/ ) { print "Found timestamp\n"; last; } } }

      The PerlMonk tr/// Advocate
      Yes... "last", that is it.
      I even dug through my old book... I can't believe I overlooked it. What a rookie error!
Re: Exiting a loop trickery
by Koosemose (Pilgrim) on Mar 11, 2004 at 01:49 UTC

    I believe the command you are looking for is last

    Just Another Perl Alchemist
Re: Exiting a loop trickery
by QM (Parson) on Mar 11, 2004 at 02:41 UTC
    TIMTOWTDI:
    LOOP_ONE: while (<>) { next unless $condition_one; last LOOP_ONE; } LOOP_TWO: while (<>) { next unless $condition_two; last LOOP_TWO; }
    The loop labels are not necessary here, but may help OMAR when the loops gain weight.

    Be careful if reading from multiple files, as the second while will start reading from the next file if the first while exits because of EOF.

    Corion pointed out that I mucked up that last paragraph.

    I should have said something like this:

    Be careful if reading from multiple files. You'll have to manage checking the end of each file yourself using continue. For instance, if you want to handle the first file with one while, and the second file with another, you might do something like this:
    while (<>) { print "$ARGV: $.: $_"; } continue { if ( eof ) { close ARGV; last; } } print "\nnext While\n\n"; while (<>) { print "$ARGV: $.: $_"; } continue { if ( eof ) { close ARGV; last; } } print "\nDone\n";
    which outputs something like this:
    X:\>while_eof file1 file2 file1: 1: file1 file1: 2: line2 file1: 3: line3 next While file2: 1: file2 file2: 2: line2 file2: 3: line3 Done
    Notes:
    * There is a difference between eof and eof() - see perldoc -f eof.
    * The line numbers in $. are reset by doing an explicit close ARGV - see perldoc perlvar.
    * If using while (<>), close ARGV won't exit the while, you'll need to do a last inside the continue.

    [I hope I've been sufficiently penitent :)]

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of