in reply to incrementing a column

my $last; my $i = 0; while (<IN>) { chomp; $i++ if (!defined($last) || $_ ne $last); $last = $_; print OUT ("$_ $i\n"); }

One liner:

perl -lpe "$i++ if ($_ ne $last); $last = $_; $_.=' '.$i;" inputfile > + outputfile

(Swap ' for " and vice-versa if you're not using Windows.)

Update: eek, that's what I get for not testing. Added the missing $last=$_.

Replies are listed 'Best First'.
Re^2: incrementing a column
by Fletch (Bishop) on Sep 28, 2004 at 14:22 UTC

    In your one liner ITYM if( $_ ne $last ) { $i++; $last = $_ }.