I have a code like this:
use Data::Dumper;
my %hash = (
'chrX' => {
'b' => [
'-51811268 210',
'-51810794 350',
'-51809935 298'
],
'f' => [
'51929018 210',
'51929492 350',
'51930351 298'
]
}
);
foreach my $cnam ( keys %hash ) {
my @lpos_f = ();
my @lpos_b = ();
if ( $hash{$cnam}{"f"} ) {
@lpos_f = @{ $hash{$cnam}{"f"} };
print "+\n";
print Dumper \@lpos_f;
}
elsif ( $hash{$cnam}{"b"} ) {
@lpos_b = @{ $hash{$cnam}{"b"} };
print "-\n";
print Dumper \@lpos_b;
}
}
Why it didn’t give print output in each ELSIF condition such
that it gives both these.
+
[
'51929018 210',
'51929492 350',
'51930351 298'
];
-
['-51811268 210',
'-51810794 350',
'-51809935 298'
];
Currently It only gives “+” output
Because
%tempis not%hash.use strictwould have told you.Moreover, you cannot get both of
if / else. Either the condition is true and you get the first part, or it is not and you get the else part. (Withelsif, the second condition might be not true as well and you get nothing).