Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Question about reading specific line in a text file

by nagalenoj (Friar)
on Apr 04, 2009 at 06:22 UTC ( #755405=note: print w/ replies, xml ) Need Help??


in reply to Question about reading specific line in a text file

Try this..,

open (DATAFILE3, $prog); @lines = <DATAFILE3>; close(DATAFILE3); $lines[76] =~ s/^(\S+\s+\S+).*/$1/; print "$lines[76]><br>\n"

Also, it is not a good practice to read the entire file in a single attempt. Think, what happens if the file is huge in size?

Try something like this to avoid reading the file like the above

open (DATAFILE3, $prog); while (<DATAFILE3>) { next if 1 .. 75; # flip-flop operator $line = $_; last; } # $line has the 76th line, do your stuff now. close(DATAFILE3);


Comment on Re: Question about reading specific line in a text file
Select or Download Code
Replies are listed 'Best First'.
Re^2: Question about reading specific line in a text file
by naikonta (Curate) on Apr 04, 2009 at 07:03 UTC
    Actually, you only need one control statement if you know exactly the line number you want to process, by using $. aka $INPUT_LINE_NUMBER in English.
    while (<DATAFILE3>) { $line = $_, last if $. == 76; }

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://755405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2015-07-31 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls