Home > Archive > PERL Beginners > August 2005 > I/O: Can't Output to a File
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
I/O: Can't Output to a File
|
|
| Ryan Frantz 2005-08-22, 6:58 pm |
| Perlers,
I'm stumped; I have a script that should output to a file but it
doesn't. The file is created, but it's empty after the script
completes. To be sure I was getting some sort of output, I had the
script write to the terminal and all was well. The odd thing, however,
is that I still can't redirect it into a file! Any clues? My script is
below.
--begin script--
#!/usr/bin/perl
use strict;
use warnings;
my $missing_claims =3D "missing_claims.txt";
my $ih_file =3D "ih.new";
my $output =3D "matches.log";
my @claims;
open(MISSING, $missing_claims) or die "Unable to open
$missing_claims:$!\n";
chomp(@claims =3D <MISSING> );
close MISSING;
my @ih;
open(IH, $ih_file) or die "Unable to open $ih_file:!\n";
@ih =3D <IH>;
close IH;
open(LOG, ">>$output") or die "Unable to open $output:!\n";
print LOG "------------ ------ ------- ----------\n";
print LOG " Image Name Loc. Removed New Loc. \n";
print LOG "------------ ------ ------- ----------\n";
foreach my $claim (@claims) {
# print "CLAIM: $claim\n";
my @matched =3D grep /$claim/, @ih;
if ( @matched ){
print LOG "@matched";
#print "@matched";
}
# my $num_matched =3D @matched;
# print "Claim matched: $num_matched: @matched\n";
}
close LOG;
--end script--
ry
| |
| John W. Krahn 2005-08-22, 9:56 pm |
| Ryan Frantz wrote:
> Perlers,
Hello,
> I'm stumped; I have a script that should output to a file but it
> doesn't. The file is created, but it's empty after the script
> completes. To be sure I was getting some sort of output, I had the
> script write to the terminal and all was well. The odd thing, however,
> is that I still can't redirect it into a file! Any clues? My script is
> below.
>
> --begin script--
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $missing_claims = "missing_claims.txt";
> my $ih_file = "ih.new";
> my $output = "matches.log";
>
> my @claims;
> open(MISSING, $missing_claims) or die "Unable to open
> $missing_claims:$!\n";
^^
**
> chomp(@claims = <MISSING> );
> close MISSING;
>
> my @ih;
> open(IH, $ih_file) or die "Unable to open $ih_file:!\n";
^
*
> @ih = <IH>;
> close IH;
>
> open(LOG, ">>$output") or die "Unable to open $output:!\n";
^
*
Your first die statement includes the $! variable, the other two should
include it as well.
> print LOG "------------ ------ ------- ----------\n";
> print LOG " Image Name Loc. Removed New Loc. \n";
> print LOG "------------ ------ ------- ----------\n";
>
> foreach my $claim (@claims) {
> # print "CLAIM: $claim\n";
> my @matched = grep /$claim/, @ih;
The only problem I can see is that $claim may contain regular expression
meta-characters that may cause the match to fail. You should probably use
quotemeta.
my @matched = grep /\Q$claim/, @ih;
> if ( @matched ){
> print LOG "@matched";
Do you really want to enclose @matched inside quotes?
perldoc -q "Why do I get weird spaces when I print an array of lines"
> #print "@matched";
> }
> # my $num_matched = @matched;
> # print "Claim matched: $num_matched: @matched\n";
> }
>
> close LOG;
John
--
use Perl;
program
fulfillment
| |
| RobertS 2005-08-23, 9:55 pm |
| I've got the same problem. This code utilizes the Yahoo::Search
module, and will print to the DOS box, when I uncomment out the line
before the next statement, and comment out the one before it, that
starts with printf, but won't print to OUT.
use Yahoo::Search;
use LWP::Simple;
#set count to 50 for maximum speed.
##
## WARNING: DANGEROUS DANGEROUS DANGEROUS
##
if (my $Response = Yahoo::Search->Query(Image => 'Britney', AppId =>
"stalagrs", Count =>10))
{
open(OUT, '>urlthumb.txt') or die "Couldn't open file.";
binmode OUT;
while (my $Result = $Response->NextResult(AutoContinue=>1)) {
printf OUT "%s\n", $Result->ThumbUrl if $Result->I < 15;
#printf "%s\n", $Result->ThumbUrl if $Result->I < 50;
}
{next}
open (OUT, "urlthumb.txt") or die "Couldn't open.";#open for reading
binmode OUT;#in binmode for Windows reading due to \n
@urls = <OUT>;
$url_ref = \@urls;
for ($i = 0; $i <=$#$url_ref; $i++) {
getstore("$url_ref->[$i]", "$i.jpg");
}
}
close(OUT);
| |
| RobertS 2005-08-23, 9:55 pm |
| I think I've found out what the problem is. I need to use print
instead of printf to print to the file.
|
|
|
|
|