file output failing when printing in ian nner block

Discussion in 'Perl Misc' started by nymphnode@gmail.com, Nov 25, 2005.

  1. Guest

    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!
     
    , Nov 25, 2005
    #1
    1. Advertising

  2. 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/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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 25, 2005
    #2
    1. Advertising

  3. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 25, 2005
    #3
  4. Guest

    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
     
    , Nov 25, 2005
    #4
  5. 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
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
     
    Eric J. Roode, Nov 26, 2005
    #5
  6. robic0 Guest

    On 25 Nov 2005 06:33:25 -0800, 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!
     
    robic0, Nov 26, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morrell
    Replies:
    1
    Views:
    966
    roy axenov
    Oct 10, 2006
  2. Replies:
    2
    Views:
    594
  3. Paul
    Replies:
    6
    Views:
    276
  4. Daniel Berger
    Replies:
    5
    Views:
    121
    Daniel Berger
    Oct 10, 2006
  5. jacob navia

    Open letter to Ian Collins

    jacob navia, May 26, 2012, in forum: C Programming
    Replies:
    32
    Views:
    833
    jacob navia
    Jun 14, 2012
Loading...

Share This Page