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";).
| [reply] [d/l] [select] |
|
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);
| [reply] [d/l] |
|
"More specifically"? What's wrong with using $.?
| [reply] [d/l] |
|
|
|
|
|
|
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..
| [reply] |
|
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.
| [reply] |
(ichimunki) Re: Determining what line a filehandle is on
by ichimunki (Priest) on Jul 06, 2001 at 00:06 UTC
|
| [reply] [d/l] |
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!
| [reply] [d/l] [select] |
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. | [reply] |
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
| [reply] [d/l] |
Re: Determining what line a filehandle is on
by snafu (Chaplain) on Jul 06, 2001 at 01:52 UTC
|
open (FILE, "file.txt");
while(<FILE>){
print "I am on line: ".__LINE__."\n";
}
close(FILE);
----------
- Jim | [reply] [d/l] |
|
Actually __LINE__ will return the line of the perl code you are on, not the line of the file opened.
| [reply] |
|
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"
| [reply] |
|
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
| [reply] |