Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending then Reading

Status
Not open for further replies.

dave498186

Technical User
Aug 23, 2009
1
US
First of let me start off by saying that I'm new to perl and new to the forums. Been self learning it for about 3 weeks on and off. Just started off making some basic programs such as a dice rolling game and a check book.

My question is for the check book. I have 3 entries in the checkbook.txt file already. In my program I access it and want to append a new withdrawl to the checkbook.txt file and then at the end of the program open up the updated file and print for visual proof of an update.

However, after updating the file and then printing, it only prints the last line that was in the .txt file before it appended. So I was hoping someone would be able to check the coding and see what I was doing wrong. It's only a few lines. Thanks in advance.

Code:
#! usr/bin/perl


open(OUTPUT, '>>checkbook.txt') || die "Couldn't open the requested file';
print OUTPUT "Withdrawl \$150\n";
close OUTPUT;

open (UPDATED, 'checkbook.txt') || die "Couldn't open the requested file';
$OpenFile = <UPDATED>;
print $OpenFile;
close UPDATED;
 
One, of several ways is this:
Code:
open UPDATED, "<", "checkbook.txt";
@OpenFile = <UPDATED> || die "Couldn't open the requested file";
close (UPDATED);

foreach my $line (@OpenFile) {
print "$line\n";
}
Get in the habit of using three argument form of open.
It has much better security (against bad people, not bad code).
You should also, for your own debugging benefit, skip using defaults on open, or you may spend hours looking for a tiny mistake.

You may also use something like this:
Code:
while (<FILE>) { 
print $_;
}

P.S., you also ended your die lines with single quote instead of double quote.
 
However, after updating the file and then printing, it only prints the last line that was in the .txt file before it appended

Your code should be printing the first line of the file, not the last, either before or after appending new data to the file.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It should also be pointed out the difference between these two lines:

Code:
$OpenFile = <UPDATED>; # scalar context
@OpenFile = <UPDATED>; # array context

In scalar context it only reads a single line from the filehandle UPDATED. It's not necessarily the first line, just the "next" line waiting to be read (the first time you do this, it's the first line; then the second line; and so-on; see the typical usage for "<STDIN>" to get user input). You can modify a Perl global so that this will slurp the whole contents of the file into $OpenFile, but by default it's 1 line at a time.

In array context it reads the whole file in, and each line in the file is an element in the @OpenFile array.

In both cases, the lines (& array elements) might end with line break characters (\n), if the line in question has another line that follows it (like hitting Return in your text editor at the end of the line) - so be sure to chomp() every line if you don't want those trailing line breaks (rarely would you need the line breaks).

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
I just solved this problem using Tie::File. It was the only way I could correctly insert a new line into a file.

In my case I needed to replace the first line.

Code:
use Tie::File;
tie @anArray, 'Tie::File', "afile.csv" or die "can't open";
#Deletes the first line in the file
shift(@anArray);
#Inserts my new line at the beginning of the file
unshift @anArray, $newLine;
#release the array
untie @anarray;

Sera
 
Well..

My problem is likely different than yours. But google led me to this post to solve my problem. I just posted my solution so some other lost soul (other than myself) could find help!

Sera

Sera
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top