Welcome to Perl and the Monastery, hghosh!

it will return a completely empty file

If you don't have a use autodie; at the top of the script, then you are not checking for errors in creating the file. The typical way to do this would be e.g. open my $fh, '>', $filename or die "$filename: $!";. Other than that, based on the code you've shown, the only other reason the first piece of code would not produce any output would be that the loops don't run because %chrsize has no entries or all its values are less than 0 - you can check this by using a tool like Data::Dumper to look at the hash (see also the Basic debugging checklist). Also, just to be safe: are you doing a close OUT; at the appropriate time?

wouldn't Perl automatically set "non-existent" keys to zero/undefined?

When attempting to access a hash key that does not exist, Perl will return undef. Therefore the two pieces of code are not equivalent: the first sets $piRNA{$chr}{$index} to 0 if there was previously no key $index, and then prints the line unconditionally, while the second piece of code only prints the output line if the key $index exists in the hash %{ $piRNA{$chr} }. In general:

If I get rid of the if statement ... It will return the $chr and $index formatted correctly, but will not print the last value.

Sounds to me like $piRNA{$chr}{$index} might be undef or "", which you can see with the aforementioned Data::Dumper, and also, if it's undef you should be seeing warnings about uninitialized values (one of the reasons Use strict and warnings is a best practice). However, if I understand your description correctly, this still doesn't explain the empty output file.

If you could use a tool like Data::Dumper or Data::Dump to get the contents of %chrsize, then you could use this to build a Short, Self-Contained, Correct Example - a short piece of code that we can run to reproduce the problem on our end.

Update 2019-08-17: Updated the link to "Truth and Falsehood".


In reply to Re: If Condition causes totally blank Output File/Issue in Writing to Output by haukex
in thread 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.