in reply to 'rewinding' file to get value from previous line in file?

This should get you where you want to go:

#!/usr/bin/perl use strict; my @Z; my $last_stop; while (<DATA>) { chomp; my @token = split ' '; if ( $token[0] eq 'Z' ) { if ($token[1] == $last_stop + 1) { $Z[-1]->[1] = $token[2]; } else { push @Z, [ @token[1,2] ]; } $last_stop = $token[2]; } } print Dumper(\@Z); __DATA__ Z 5 89 Z 92 102 Z 103 123 Z 126 150
-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: 'rewinding' file to get value from previous line in file?
by ikegami (Patriarch) on Dec 07, 2006 at 19:00 UTC
    • $last_stop gives an undefined warning.
    • $last_stop can be replaced with $Z[-1][1].
    • chomp is redundant.
    • The if is fine as it is, but the common $Z[-1][1] = $token[2] can be factored out.
    my @Z; while (<DATA>) { my @token = split ' '; if ( $token[0] eq 'Z' ) { push @Z, [ $token[1] ] if !$Z[-1] || $token[1] != $Z[-1][1] + 1; $Z[-1][1] = $token[2]; } }
    A reply falls below the community's threshold of quality. You may see it by logging in.