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
In reply to Inserting a line in the middle of a text by goude
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |