in reply to Inserting a line in the middle of a text

Where to start. First, what you are trying to do is invalid. Unless you have fixed length records, it is not possible to insert a record into the middle of a text file. That is the case regardless of the language. The best you can do is to read a file then write out to a different one, inserting the new record in the correct place in the new file.

Opening a file for append and read will position the current file position to end-of-file, so you will not read anything.

The open statement requires parentheses around the argument list when you use the high precendent || operator. Or use the low precedence or.

print on its own writes to STDOUT, not to a file.

By convention, it is not a good idea to use UPPERCASE for your own variable names, like $NAMES.

This may get you what you want:
#!/usr/bin/perl use strict; use warnings; my $infile = '/home/yanni/scripts/testfiles/list_names'; my $outfile = "$infile.tmp"; open (my $in, '<', $infile ) || die "Can't open $infile $!\n"; open (my $out, '>', $outfile) || die "Can't open $outfile $!\n"; while (<$in>) { print $out $_; if ($.== 3) {print $out "Simon\n";} } close ($in); close ($out); rename ($outfile, $infile) || die "Unable to rename: $!";
Update: On second thought, the record length has nothing to do with it if you want to insert (I was thinking of an overwrite). You would still have to read the records in first somehow.

Replies are listed 'Best First'.
Re^2: Inserting a line in the middle of a text
by JavaFan (Canon) on Jan 20, 2009 at 15:13 UTC
    Where to start. First, what you are trying to do is invalid. Unless you have fixed length records, it is not possible to insert a record into the middle of a text file. That is the case regardless of the language.
    You are right that it's not language dependent. It's file system dependent. Most Unix and Windows filesystems where files are just streams of bytes - and there it's not possible to insert records without copying sections of the file. But there are filesystems where files are lists of records - files look a bit like row-based database tables. IIRC, VMS has the ability to use such filesystems.

    But the above is just some pedantery, chances the OP is using such a file system are tiny.

Re^2: Inserting a line in the middle of a text
by goude (Initiate) on Jan 20, 2009 at 12:17 UTC
    Thank you very much cdarke for explaining my mistakes.

    Your script works perfectly.

      You start the thread by saying:

      I've been reading a not very useful book for a few weeks now.

      and it strikes me that the number of problems in the OP script might be an indicator of just how "not very useful" that book really is. Have you checked out the Reviews section here at the Monastery? Is your book already reviewed there? (If not, perhaps you could take your revenge on the "not very useful" author by contributing a review of the book.)

      Oh, and maybe you can find a better book... (maybe our Book Reviews nook will help you find one). Personally, my own favorite reading has always been the documentation that comes with perl -- starting perhaps with perl.