Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
XP is just a number
 
PerlMonks  

Going to a specific line in a file.

by Jayesh Nair (Initiate)
on Jun 13, 2007 at 13:58 UTC ( #620965=perlquestion: print w/ replies, xml ) Need Help??
Jayesh Nair has asked for the wisdom of the Perl Monks concerning the following question:

monks, How can we go(read) to a specific line in a file? For example if i have to read the 15th line of the file.. Usually we can go only in a order starting from the first line,right.How can i directly raed a specific line? Help required urgently!!!!!!

Comment on Going to a specific line in a file.
Re: Going to a specific line in a file.
by fenLisesi (Priest) on Jun 13, 2007 at 14:01 UTC
    Unless your lines are of constant length, you have to go line by line. Cheers.
Re: Going to a specific line in a file.
by marto (Chancellor) on Jun 13, 2007 at 14:04 UTC
Re: Going to a specific line in a file.
by citromatik (Curate) on Jun 13, 2007 at 14:05 UTC

    You can use the Tie::File module

    use strict; use warnings; use Tie::File; my $file = shift @ARGV; tie my @arr, 'Tie::File', $file; #Update: (thanks Corion and JohnGG) #print $arr[15]."\n"; print $arr[14]."\n";

    citromatik

      print $arr[15]."\n";

      <nit>That is the 16th line.</nit>

      Also, why not just interpolate into the string rather than using concatenation?

      print qq{$arr[14]\n};

      Cheers,

      JohnGG

Re: Going to a specific line in a file.
by ikegami (Pope) on Jun 13, 2007 at 14:13 UTC

    Tie::File still reads line by line, and has a lot of overhead. Alternative:

    do { die("Premature EOF") if not defined <FILE>; } while $. < 14; my $line15 = <FILE>;
Re: Going to a specific line in a file.
by Anonymous Monk on Jun 13, 2007 at 19:38 UTC
    #!/usr/bin/perl open file,"./testFile.txt" or die; while (<file>) { next if (1..6); --> this skips the first 6 lines. print "$_\n"; }
Re: Going to a specific line in a file.
by Spidy (Chaplain) on Jun 13, 2007 at 23:26 UTC
    This sounds like a homework question to me...
      May be for u it may seem like a homework question, bt not for me..it was one doubt i encountered during my coding.. It may be because our wisdom levels are far apart in magnitude..... Hatsoff Mr Spidy....
      I think this is a place where we institute ourselves to help each other.... rather than to make fun of others Mr Spidy..
        Don't be angry, actually, You've found what you what.

        UPDATE correct stupid mistake according to -b's message.

        I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
Re: Going to a specific line in a file.
by ctilmes (Priest) on Jun 14, 2007 at 11:28 UTC
Re: Going to a specific line in a file.
by Asmo (Monk) on Jun 14, 2007 at 20:56 UTC
    Simplest method :
    open (DATAFILE, $filename); @lines = <DATAFILE>; close(DATAFILE); print $lines[14];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2013-05-22 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (469 votes), past polls