Hello, I'm fixing a perl script for the lab I work at. This script used to work before our linux cluster underwent some update in 2017 (just for background). I'm workshopping each section of my script. I'm a perl novice, by the way! Everything before this block of code is working. The block of code below should open a file, find the $size of a chromosome from %chrsize, and iterate through a range of (0-$size). Currently, I've identified this strange issue: it will return a completely empty file. If I get rid of the if statement (where $piRNA{$chr}{$index} is set to zero), then the output file is still incorrect. It will return the $chr and $index formatted correctly, but will not print the last value.

open OUT, ">", "$filename.counts"; foreach $chr (keys %chrsize){ $size = $chrsize{$chr}; # chr, size, index work (print statements + show reasonable values) foreach $index (0..$size) { $piRNA{$chr}{$index}=0 if (!exists $piRNA{$chr}{$index}); + print OUT "$chr\t$index\t$piRNA{$chr}{$index}\n"; } }

If I use an if-elsif statement, it still won't work. The only way I've gotten viable results is to specify that the script print to OUT only if $piRNA{$chr}{$index} exists. Can't I simply delete everything under the second foreach statement except for the "print OUT" statement, and wouldn't Perl automatically set "non-existent" keys to zero/undefined?

open OUT, ">", "$filename.counts"; foreach $chr (keys %chrsize){ $size = $chrsize{$chr}; # chr, size, index work (print statements + show reasonable values) foreach $index (0..$size) { if (exists $piRNA{$chr}{$index}) { print OUT "$chr\t$index\t$piRNA{$chr}{$index}\n"; } } }

I'd really appreciate any help or guidance, because my brain is fried.


In reply to If Condition causes totally blank Output File/Issue in Writing to Output by hghosh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.