For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Re: Prefix printed lines on screen with a line number. Need advice.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Prefix printed lines on screen with a line number. Need advice.
DJ Stunks

2006-10-01, 6:58 pm

Jay wrote:
> Ok, so forget all that code then. Obviously I am so far off.


Don't get upset, there was some good advice there. Such as when it was
suggested to use strict; use warnings; and use the 3-argument form of
open();

> Let's
> focus on the original problem. I have to write a perl script for a
> class which reads a file and prints its contents on screen and prefixes
> each line with a line numbers. I know how to read the file and print
> it properly, but how do I prefix each line with a line number?
>
> #!/usr/bin/perl -w
>
> open (FILE, "$ARGV[0]") or die "Can't open $ARGV[0]\n";
>
> while ($lines = <FILE> ) {
> print $lines;
> }
>
> close (FILE);


read perldoc perlvar and look for the special Perl variable to do with
"current line number".

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com