Regular expression – Perl if statement confusion

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{< br />$end =$data[$i][1];
}
print $end;

This is an alternative expression that does not 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 gives me the expected result, but the other (the last line) it does not work; it always Execute the else statement.

Question: What is the cause of this problem? How to deal with it?

Since you are trying to assign one of the two values ​​to $end based on conditions, why not write The conditional expression is as follows:

$end = ($rtgene !~ /trn/)? $data[$i][0]+100: $data[$i][1];
print $end;

This avoids the priority problem that ysth accurately diagnoses and is easier to understand.

Perl is available To write incredible code; however, it is not a good idea to make all the code difficult to understand. Stacking and printing jobs on one line tends to be incredible.

This code It 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 does not work:

($rtgene !~/trn/)? $end=$data[$i][0]+100: $end=$data[$i][1]; print $end;

In one code (normal if else statement) it works and gives me the expected result, but in the other (the last line) it does not work; it always executes the else statement.

Question: this What is the cause of the problem? How to deal with it?

Since you are trying to assign one of the two values ​​to $end based on a condition, why not write a conditional expression as follows:

< /p>

$end = ($rtgene !~ /trn/)? $data[$i][0]+100: $data[$i][1];
print $end;

This avoids priority issues that ysth accurately diagnoses and is easier to understand.

Perl can be used to write incredible code; however, make all code It’s not a good idea to be hard to understand. Stacking and printing jobs on one line tends to be incredible.

Leave a Comment

Your email address will not be published.