file output failing when printing in ian nner block

N

nymphnode

Hi,

I'm not sure what I'm doing wrong with my output redirection, and I've
done something similar before which had worked so I'm at a loss. This
is the part of my code that is not working:

------ *snip * ----------
foreach my $sub ( keys %RTs ) {
$out = $sub".1d";

open(OUT,">$out") || die "Cannot open $out to write: $!\n";

print OUT "yay";

if ( $sub eq "key") {
print OUT "booo";
print "in if clause";
}
close(OUT);
}
-----------------------------

Basically, it will print "yay" (before the if block) into the output
file, but it won't print "booo" (in the if block) to the output file
even though I'm obviously going into the loop because I get "in if
clause" printed to the terminal. It seems like the output file is
closing when I get into the if loop...why and how do I get around this?
Or is there something else I'm doing wrong?

Thanks!
 
G

Gunnar Hjalmarsson

I'm not sure what I'm doing wrong with my output redirection, and I've
done something similar before which had worked so I'm at a loss. This
is the part of my code that is not working:

------ *snip * ----------
foreach my $sub ( keys %RTs ) {
$out = $sub".1d";

open(OUT,">$out") || die "Cannot open $out to write: $!\n";

print OUT "yay";

if ( $sub eq "key") {
print OUT "booo";
print "in if clause";
}
close(OUT);
}
-----------------------------

Basically, it will print "yay" (before the if block) into the output
file, but it won't print "booo" (in the if block) to the output file
even though I'm obviously going into the loop because I get "in if
clause" printed to the terminal. It seems like the output file is
closing when I get into the if loop...why and how do I get around this?
Or is there something else I'm doing wrong?

Probably the latter, but there is no way we could tell, is it?

Please post a short but _complete_ program, that people can copy and
run, and that illustrates the problem you are having - just as is
recommended in the posting guidelines for this Usenet group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

When you post next time, please comply with the posting guidelines also
in other respects. For instance, strictures and warnings should be
enabled in the program you post.
 
T

Tad McClellan

$out = $sub".1d";


That is a syntax error. Your program does not even compile.

it will print "yay"


I doubt that, since "it" will not even execute...

Or is there something else I'm doing wrong?


Yes, probably.

But the "something else" is likely in code or data that you
have not shown us, so we can't help with that part.


Have you seen the Posting Guidelines that are posted here frequently?
 
X

xhoster

Hi,

I'm not sure what I'm doing wrong with my output redirection, and I've
done something similar before which had worked so I'm at a loss. This
is the part of my code that is not working:

------ *snip * ----------
foreach my $sub ( keys %RTs ) {

There is no %RTs.
$out = $sub".1d";

You don't appear to be using strict. Please do so. Are you using
warnings? If not, do that too.
open(OUT,">$out") || die "Cannot open $out to write: $!\n";

print OUT "yay";

print OUT "yay" or die $!.
if ( $sub eq "key") {
print OUT "booo";

or die $!;
print "in if clause";
}
close(OUT);

or die $!;
}
-----------------------------

Basically, it will print "yay" (before the if block) into the output
file, but it won't print "booo" (in the if block) to the output file
even though I'm obviously going into the loop because I get "in if
clause" printed to the terminal. It seems like the output file is
closing when I get into the if loop...why and how do I get around this?
Or is there something else I'm doing wrong?

Don't ask us first, ask Perl first.

Xho
 
E

Eric J. Roode

(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
------ *snip * ----------
foreach my $sub ( keys %RTs ) {
$out = $sub".1d";

open(OUT,">$out") || die "Cannot open $out to write: $!\n";

print OUT "yay";

if ( $sub eq "key") {
print OUT "booo";
print "in if clause";
}
close(OUT);
}
-----------------------------

Basically, it will print "yay" (before the if block) into the output
file, but it won't print "booo" (in the if block) to the output file
even though I'm obviously going into the loop because I get "in if
clause" printed to the terminal. It seems like the output file is
closing when I get into the if loop.

Seems to me the if() condition must be false. Why not print $sub?

print OUT "yay: [$sub]";

Better yet, why not single-step through the program with a debugger? I
bet that makes the cause of the problem obvious.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
R

robic0

Hi,

I'm not sure what I'm doing wrong with my output redirection, and I've
done something similar before which had worked so I'm at a loss. This
is the part of my code that is not working:

------ *snip * ----------
foreach my $sub ( keys %RTs ) {
$out = $sub".1d";
^
won't compile, $out = $sub.".1d";
?
Didn't read any other post, I assume this is the problem...
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top