goude has asked for the wisdom of the Perl Monks concerning the following question:
Hope you are all well. I'm relatively new to perl and I've been reading a not very useful book for a few weeks now. I have just finished learning "writing and reading files".
At the end of this chapter there are exercises and one of them is to insert a line in the middle of a file but the script I wrote does nothing.
The file I have looks like this:
Jonathan Peter Jason Nick Stela Samantha
And I want to insert the name Simon below Jason and above Nick without overwriting any of the existing names.
Jonathan Peter Jason Simon Nick Stela Samantha
The script I wrote is below but it doesn't print anything in the file and it doesn't give me any warnings or errors.
my $file = '/home/yanni/scripts/testfiles/list_names'; open my $NAMES, '+>>', $file || die "What the hell? Can't open file $! +\n"; while (<$NAMES>) { if ($.== 3) {print "\nSimon\n";} } close ($NAMES);
First, I open the file for reading and appending (+>>), then with the while command I read the file line by line (at least this is what it says in the book). Inside the while loop I "state" that when you read line 3 print the name Simon.
What am I doing wrong?
Any suggestions please?
Thanks a lot
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inserting a line in the middle of a text
by cdarke (Prior) on Jan 20, 2009 at 12:02 UTC | |
by JavaFan (Canon) on Jan 20, 2009 at 15:13 UTC | |
by goude (Initiate) on Jan 20, 2009 at 12:17 UTC | |
by graff (Chancellor) on Jan 21, 2009 at 02:18 UTC | |
|
Re: Inserting a line in the middle of a text
by swampyankee (Parson) on Jan 20, 2009 at 12:20 UTC | |
|
Re: Inserting a line in the middle of a text
by scorpio17 (Canon) on Jan 20, 2009 at 15:28 UTC | |
by davido (Cardinal) on Jan 20, 2009 at 17:15 UTC | |
by ikegami (Patriarch) on Jan 20, 2009 at 17:34 UTC |