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.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |