Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Number Line while reading by chunks

by ambs (Pilgrim)
on Aug 18, 2008 at 11:05 UTC ( #704907=perlquestion: print w/replies, xml ) Need Help??

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


When we use a different record separator (for instance, an empty new line), the variable dollar dot ($.) does not count the number of lines read, but the number of chunks read.

My question is if there is any automatic method to get the line number where a chunk starts (I know, and I am able to do it manually. Just to be sure I am not re-inventing the wheel).


Alberto Simões

Replies are listed 'Best First'.
Re: Number Line while reading by chunks
by massa (Hermit) on Aug 18, 2008 at 11:20 UTC
    Olá, Alberto. in short, no -- you have $., the current record number, and the result of tell, the current byte number. Maybe it would be better to trust perl's buffering and code your thing in a line-oriented way?? (just a suggestion, of course...)
    []s, HTH, Massa (κς,πμ,πλ)
Re: Number Line while reading by chunks
by JavaFan (Canon) on Aug 18, 2008 at 11:43 UTC
    $. always counts the number of chunks read. It's just that by default the chunks happen to be lines.
Re: Number Line while reading by chunks
by Perlbotics (Bishop) on Aug 18, 2008 at 11:52 UTC
    Maybe the easiest way is to count the lines on your own?
    perl -ne 'BEGIN {our $n=1; $/="";}; print "=Line:$n, Record:$.==\n$_"; + $n += y/\n/\n/;'< textfile
    Update: Oops, missed something when doing the test. Code above corrected. This was formerly
    perl -ne '$/=""; $F{1} += y/\n/\n/ -1; print "=Line:",$F{1},", Record: $.==\n$_==\n"; ' < textfile
    Sorry for that.

      Yup, that was what I was thinking on. But you know, Perl has so many variables and tweaks, that I might be forgetting something.

      Thanks ;)

      Alberto Simões

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://704907]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2021-09-28 16:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found