I’m just trying to check if a string is equal to “%2B”, and if it does I change it to “+”.
The problem lies in comparison.
if ($lastItem == "%2B"){
$lastItem = "+";
}
When $lastItem is something completely different (like “hello”), it will still go into the statement. I’ve been wracking my brain and I just can’t tell where I’ve gone wrong. Does %2B have some special meaning? I’m very new to perl.
Thanks
You need to use
eqwhen comparing strings, or perl will try to convert the string to a number (which will be0), and you will find such oddities as"a" == 0to evaluate true. And when comparing two strings, you will of course effectively getif (0 == 0), which is the problem you are describing.It is important to note that if you had used
use warnings, this problem would have been easier to spot, as this one-liner will demonstrate: