For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2005 > file output failing when printing in ian nner block









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 file output failing when printing in ian nner block
nymphnode@gmail.com

2005-11-25, 6:59 pm

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!

Gunnar Hjalmarsson

2005-11-25, 6:59 pm

nymphnode@gmail.com wrote:
> 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/c...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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
xhoster@gmail.com

2005-11-25, 6:59 pm

nymphnode@gmail.com wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Tad McClellan

2005-11-25, 6:59 pm

nymphnode@gmail.com <nymphnode@gmail.com> wrote:


> $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?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Eric J. Roode

2005-11-26, 7:56 am

nymphnode@gmail.com wrote in news:1132929205.116405.148100
@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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
robic0

2005-11-26, 7:56 am

On 25 Nov 2005 06:33:25 -0800, nymphnode@gmail.com wrote:

>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...
>
> 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!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com