Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Display Line Numbers
by davido (Cardinal) on Jan 30, 2004 at 04:15 UTC
    You may be talking about line numbers in a text editor used to edit Perl scripts. If that's the case, you're asking a broad question because we have no idea what code editor you're using (and since it's not a Perl topic we probably don't much care).

    Or you may be asking how to determine what line number you've just read out of a file. If that's the case, the "$." special variable will tell you what line number you're reading. See perlvar for details.

    Or you may be asking how to determine what line in your script is currently executing. If that's the case, the __LINE__ tag will tell you. You can print __LINE__, "\n";, for example. See perldata for details about the __LINE__ "Special Literal".

    Or if you just want to know how many lines there are in a file.... well, here's a Perl solution:

    perl -n -e "END { print $., "\n"; }" file.txt


    Dave

      I am using VI on a unix box using Perl 5.x.... I thought there were a means of displaying the line numbers in your code. Maybe this is not a perl question at all. I do apologize but sincerely appreciate the responses. Thanks,

        In addition to the responses that you've already received, if you use vi to edit, then you should use the '+n' argument from the command line to put you directly to that line of the source file. Alternatively, while inside vi, type ':n' to go to line n in the file.

        And no, that wasn't a Perl question. The next time you have a vi question, read the man page or buy the O'Reilly book.

        Alex / talexb / Toronto

        Life is short: get busy!

Re: Display Line Numbers
by jweed (Chaplain) on Jan 30, 2004 at 03:48 UTC
    Not really a perl question, more like an editor question. In vim, my editor of choice, type :se nu or put se nu in you ~/.vimrc file.

    Depending on the editor, YMMV.

    Or, more suavely but less practically, use Filter::NumberLines.



    Code is (almost) always untested.
    http://www.justicepoetic.net/
Re: Display Line Numbers
by b10m (Vicar) on Jan 30, 2004 at 08:41 UTC

    On *NIX systems, by far the easiest way would be the program called `nl`, which is part of the GNU coreutils. And for those interested, see the documentation.

    Then of course nearly every GNU tool has the abillity to display line numbers, for example with `grep` and `cat` you could use the "-n" parameter to toggle the display of file numbers.

    --
    b10m

    All code is usually tested, but rarely trusted.
      Thanks for the reminder; I've seen nl before but had forgotten about it. Instead, I often use grep -vn xyzzy (or, for variety, grep -vn plugh).

      > free bird

Re: Display Line Numbers
by crabbdean (Pilgrim) on Jan 30, 2004 at 04:48 UTC
    Not sure of your system but most editors have some sort of "find lines" functionality. If you are on a Windows PC most often CTRL+G will bring up a prompt to jump to the line. Even in "notepad" on Windows CTRL-G works. Personally as an editor in Windows I use UltraEdit32. Its like a text editor on steriods. I'm pertty sure also on Unix systems "vim" has the option to display lines numbers and also an option to jump to line numbers. Conversely try running the perl debugger "perl -d <scriptname>" and type "h" for help. From here you can move through your program in live mode line by line and see your variables as you go. Enjoy!

    Dean
Re: Display Line Numbers
by crabbdean (Pilgrim) on Jan 30, 2004 at 20:52 UTC
    Although there are better solutions as has already been suggested above, here is a quick script I just wrote to output 5 lines either side a line number you specify.

    Just give it two arguments on the command line when you run it. 1. A line number you want to display, and 2. the file you want to display the line number from.

    eg. lnb.pl 14 testfile.txt

    This will display lines 9 through to 19 (5 either side of 14) from file testfile.txt

    Enjoy!
    Dean

    #!perl $nmb = shift; $file = shift; open (FILE, "<$file") or die $!; @file = <FILE>; close FILE; print "\n"; foreach (@file) { if (abs($nmb - ++$i) <= 5) { print $i, ":\t$_"; } } print "\n";
Re: Display Line Numbers
by calin (Deacon) on Jan 30, 2004 at 19:35 UTC
    In addition to nl and other standard UNIX tools, here's a Perl one-liner:

    perl -ne 'printf "%4d $_", $.'

    For counting words, characters and lines there's also a standard tool called wc.