in reply to Re: How do I parse and evaluate a file line-by-line?
in thread How do I parse and evaluate a file line-by-line?

You can tighten that middle loop up a bit by getting out of "C think" and getting into "Perl think":
while (<DATA>) { my ($time, $switch) = split "\t"; if ($switch eq $last) { $output[-1] = $_; } else { push @output, $_; $last = $switch; } }
The $cursor variable is unnecessary, because Perl always knows how long the array is.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Re: How do I parse and evaluate a file line-by-line?
by Trimbach (Curate) on Mar 03, 2001 at 20:57 UTC
    Thanks for that... I rarely use $array[$element] to grow an array, preferring "push", but I couldn't use push indiscriminately here because I needed to write over successive instances of UP or DOWN. Of course, $array[-1] was exactly what I was looking for. Gotta get used to using the relative position in an array....

    And for the record, it wasn't "C think" as I actually know absolutely nothing about C. It was just inexperience. :-D

    Gary Blackburn
    Trained Killer