Reading the nth line from a file (Perl and Tcl examples)
"How do I find the 100th line in a file" - a common question for newcomers to coding. The short answer is to open the file, and loop through to read lines until the one that you want. Although most languages have a seek
command or function, that works by bytes and with a typical text / ascii file, line lengths vary so that you don't know exactly where to seek if you try to use it.
As an alternative, in some circumstances you may wish to read the whole file into a list / array and then look by line number - in Perl:
@lines = <FH>;
or read the whole file into a string and then turn it into a list - in Tcl:
set fp [open rails_routes]
set stuff [read $fp]
set lines [split $stuff "\n"]
puts [lindex $lines 39]
but in such cases you need to be careful if the file's potentially huge.
Where a large file is accessed a lot by record number, and doesn't change often, you might consider setting up an index or (say) every thousandth line, so that you can seek
most of the way to what you want - or perhaps you should be using a database rather than a plain file if you have that requirement. Isn't it wonderful how technologys go together! (written 2011-06-09)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesP215 - Perl - More about Files 
Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15) 
Handling binary data in Perl is easy! - (2011-08-30) 
An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21) 
But I am reading from a file - no need to prompt (Perl) - (2009-09-14) 
Processing all files in a directory - Perl - (2008-10-11) 
There is more that one way - Perl - (2008-07-14) 
Perl - functions for directory handling - (2007-06-09)T209 - Tcl/Tk - File and Directory Handling 
The fileutil package and a list of file system commands in Tcl - (2012-02-18) 
Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09) 
Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04) 
Tcl - catching an error before your program crashes - (2009-10-22) 
stdout v stderr (Tcl, Perl, Shell) - (2007-12-10) 
Buffering up in Tcl - the empty coke can comparison - (2007-11-10) 
Reading from another process in Tcl (pipes and sockets) - (2007-10-26) 
Running external processes in Tcl and Tcl/Tk - (2006-06-29) 
The fragility of pancakes - and better structures - (2006-06-26)
Some other Articles
Rabbit ShelterIs Melksham town centre dying?How much has Perl (and other languages) changed?Travelling aroundReading the nth line from a file (Perl and Tcl examples)Moving on - a task for the hotel staff!Summer Sunday Trains - outings from Swindon, Chippenham, Melksham, Trowbridge and WestburySelf Portrait (in words)Twitter Phishing Trips ... and a great new alert serviceFriday - Electrician, Food Festival, C++ Course, Rail Group Meeting
4236 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).