in reply to Append to beginning of line (multi line file)

one thing you can do for each line is just substitute the start of the line for the current number. as a one liner though:
perl -pi.bak -e '$count++;s/^/$count. /' file_name
or in your could you could change:
foreach my $line (@file) { (undef) = <FILE>; # stores number of lines into $. $count++; print FILE, "$count. "; if ($count == $.) {last; } }
to something like:
foreach my $line (@file) { $count++; print FILE, "$count. $line"; }
or:
foreach (@file) { $count++; s/^/$count. /; print FILE; }
As always TMTOWTDI. I am curious as to why this line:
(undef) = <FILE>; # stores number of lines into $.
and this line:
if ($count == $.) {last; }
is necessary.

-enlil

Replies are listed 'Best First'.
Re: Re: Append to beginning of line (multi line file)
by theorbtwo (Prior) on Jan 23, 2003 at 20:43 UTC

    (undef)=<FILE>; reads lines out of <FILE>, and stores it nowhere, it's the same as <FILE>. The answer to your puzzle isn't that (undef)=<FILE> is somehow magical (other then having a list-context call that goes nowhere; ()=<FILE> would should work just as well for that), it's that $. is a special variable that holds the current (input) line number. It's updated on all reads.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).