This code is an if statement; I want to test whether $rtgene contains trn.
I tested the code with $rtgene= nad3:
if ($rtgene!~/trn/){
$end=$data[$i][0]+100;
}
else{
$end =$data[$i][1];
}
print $end;
This is an alternative expression that doesn’t work:
($rtgene!~/trn/)? $end=$data[$i][0]+100 : $end=$data[$i][1]; print $end;
The problem is in the first code (normal if else statement) it works and give me the expected results, but the alternative one (last line) it’s not working; it always executes the else statement.
Question: What is the cause of this problem? How to fix it?
Since you are trying to assign one of two values to
$endbased on a condition, why not write the conditional expression like that:This avoids the precedence problem accurately diagnosed by ysth and is quite a bit easier to understand.
Perl can be used to write inscrutable code; it is not a good idea to make all your code inscrutable, though. Stacking the assignment and print on a single line is tending towards inscrutability.