Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Determining what line a filehandle is on

by Anonymous Monk
on Jul 06, 2001 at 00:01 UTC ( [id://94243]=perlquestion: print w/replies, xml ) Need Help??

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

Very simply.. How can you determin what line of a file you are on?
open (FILE, "file.txt"); while(<FILE>){ print "i am on line..." <however you find out what line you are on> } close FILE;

Replies are listed 'Best First'.
Re: Determining what line a filehandle is on
by wog (Curate) on Jul 06, 2001 at 00:06 UTC
    The $. variable described in perlvar does exactly this. Or you could keep your own variable to count lines and increment it yourself.

    I hope that you are checking the return value of open in your real code. (e.g. open FILE, "file.txt" or die "opening file.txt for reading: $!\n";).

      More specifically:
      my $line = 0; my $file = "file.txt"; open (FILE, $file) || die "Could not open $file\n"; while (<FILE>) { print "I am on line ", ++$line, "\n"; } close (FILE);
        "More specifically"? What's wrong with using $.?
Re: Determining what line a filehandle is on
by mattr (Curate) on Jul 06, 2001 at 10:02 UTC
    As you can see from the above responses there are a few ways to do it, but there are a couple of things to watch out for if you decide to use the special Perl variable $. which is its special behavior that arises when reading from the the empty diamond operator (<>) and also when you localize the variable itself.

    Basically I am 99% sure you just want to increment a counter and keep track of the line number yourself. Since the file will open at the beginning and read one line each time the while statement loops, a simple $line++ statement inside that loop should be sufficient.

    In addition to checking that open succeeded, I'd also recommend adding \n to the end of your print statement..

      Not to mention that $. only provides you with the lineno of the last filehandle read. If you are interspersing reads from 2+ files, this can come back and bite you. In these cases, it's much easier to maintain an index for each file.

      Mark.

(ichimunki) Re: Determining what line a filehandle is on
by ichimunki (Priest) on Jul 06, 2001 at 00:06 UTC
Re: Determining what line a filehandle is on
by petdance (Parson) on Jul 06, 2001 at 07:37 UTC
    You can also use English; and use special variable $NR.

    xoxo,
    Andy
    --
    Throw down the gun and tiara and come out of the float!

Re: Determining what line a filehandle is on
by Anonymous Monk on Jul 06, 2001 at 20:34 UTC
    Easy, Set a counter and increment it each time you read a line.
Re: Determining what line a filehandle is on
by lindex (Friar) on Jul 08, 2001 at 11:14 UTC
    hey I might as well throw in my two cents worth of code ..
    #!/usr/bin/perl $|++ use strict; use warnings; open(my($fd),q(/usr/dict/words)) or die($!); print qq(\r$.) while(<$fd>); print qq(\n);

    Brought to you by that crazy but lovable guy... lindex
Re: Determining what line a filehandle is on
by snafu (Chaplain) on Jul 06, 2001 at 01:52 UTC
    I prefer __LINE__

    open (FILE, "file.txt"); while(<FILE>){ print "I am on line: ".__LINE__."\n"; } close(FILE);

    ----------
    - Jim

      Actually __LINE__ will return the line of the perl code you are on, not the line of the file opened.
      silly goose. You know what that does? :) __LINE__ tells what line of CODE you're on, not line of the filehandle :)

      eg

      01: use strict
      02:
      03: print "I am on line ", __LINE__, "\n";
      
      will always print
      "I am on line 3\n"
      
      It appears that I made a grave error in my reading of this post :) and thus was 'punished' for it (I know its not really punishment...Im just being colorful).

      Anyway, I appreciate everyone pointing this lil mistake out to me. I will be more careful in my readings of posts next time. I was in a hurry when I read and replied to this write-up.

      There are times where you just gotta admit your mistakes. Anyway, I hope I didn't cause any confusion to those who are new to Perl. IGNORE MY ANSWER! :) It is NOT correct.

      ----------
      - Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://94243]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found