Beginner Filehandle question

C

Chris L.

Dear Experienced Perl programmmer:
I am having trouble understanding exactly why I can print the below
program just fine through my IDE console, but when I try printing the
exact same array (@goose) to a text file through a file handle, it only
prints the last entry...
Again, I have pasted my code below, and would appreciate any advice as
to how to remedy this problem. I have gone over all three of my Perl
books and searched Google for the last 4 hours, and cannot find the
answer...
Thank you very much for your help
Chris

-------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;

open(ORIGINN, "C:\\Documents and Settings\\Admin\\My
Documents\\ci_p.txt")|| die "Cannot open bci_pep.txt: $!";
#opening my text file

my (@DATA)=<ORIGINN>;
close (ORIGINN);
#copied my text file to array @DATA

my@UUOO = grep /Entry/, @DATA;
#grabbed each line in my array starting with Entry

foreach my$cone (@UUOO){
my $var= substr($cone, 28, -1);
#selected a particular word from each line in UUOO array

my @goose="$var\n" ;

print @goose;
#prints out in the Active State Komodo IDE output screen PERFECTLY!

open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
print LOGF @goose;
close(LOGF);
#prints only the last entry to the text file "entry.txt :(
Howcome??
}
 
J

J. Gleixner

foreach my$cone (@UUOO){
open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
print LOGF @goose;
close(LOGF);
#prints only the last entry to the text file "entry.txt :(
Howcome??
}

Because your open() is within your foreach loop. Each time through the
loop it creates/over-write your 'entry.txt' file and write whatever was
in @goose.

If you want to append information, use '>>';

perldoc -f open
 
E

Eric Martin

Chris said:
-------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;

open(ORIGINN, "C:\\Documents and Settings\\Admin\\My
Documents\\ci_p.txt")|| die "Cannot open bci_pep.txt: $!";
#opening my text file

my (@DATA)=<ORIGINN>;
close (ORIGINN);
#copied my text file to array @DATA

my@UUOO = grep /Entry/, @DATA;
#grabbed each line in my array starting with Entry

foreach my$cone (@UUOO){
my $var= substr($cone, 28, -1);
#selected a particular word from each line in UUOO array

my @goose="$var\n" ;

print @goose;
#prints out in the Active State Komodo IDE output screen PERFECTLY!

open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
print LOGF @goose;
close(LOGF);
#prints only the last entry to the text file "entry.txt :(
Howcome??
}

Try this:
use strict;

my @goose;
open(ORIGINN, "C:/Documents and Settings/Admin/My Documents/ci_p.txt")
|| die "Cannot open bci_pep.txt: $!";
while (<ORIGINN>) {
if (/^Entry/) {
push(@goose, substr($_, 28, -1)."\n");
}
}
close (ORIGINN);
open(LOGF, ">C:/entry.txt")|| die "Cannot open entry.txt: $!";
print LOGF @goose;
close(LOGF);

Thanks,
Eric
 
M

Mark Clements

Eric said:
Try this:
use strict;

my @goose;
open(ORIGINN, "C:/Documents and Settings/Admin/My Documents/ci_p.txt")
|| die "Cannot open bci_pep.txt: $!";

so if it can't open ci_p.txt, you'll get the message
"Cannot open bci_pep.txt...."

:)

Is best to use variables in this kind of situation to avoid misleading
oneself. Also, lexically scoped filehandles are now generally advised,
rather than global filehandles.

my $filename = "C:/Documents and Settings/Admin/My Documents/ci_p.txt";
open (my $originn, "<",$filename) || die "Could not open $filename for
read: $!";

while(<$originn>){
.....

regards,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top