我只是想检查一个字符串是否等于"%2B",如果是,我就把它改成"+“。问题在于比较。
if ($lastItem == "%2B"){
$lastItem = "+";
}当$lastItem是完全不同的东西(比如"hello")时,它仍然会进入语句。我一直绞尽脑汁,就是说不出哪里出错了。%2B有什么特殊的含义吗?我是perl的新手。
谢谢
发布于 2012-02-13 13:25:20
在比较字符串时,您需要使用eq,否则perl将尝试将字符串转换为一个数字(将是0),您将发现像"a" == 0这样的奇怪之处来计算true。当比较两个字符串时,你当然会得到if (0 == 0),这就是你所描述的问题。
if ($lastItem eq "%2B") {重要的是要注意,如果您使用了use warnings,这个问题会更容易发现,就像下面这行代码所演示的那样:
$ perl -wE 'say "yes" if ("foo" == "bar")'
Argument "bar" isn't numeric in numeric eq (==) at -e line 1.
Argument "foo" isn't numeric in numeric eq (==) at -e line 1.
yes发布于 2012-02-13 18:26:37
我认为你真的想要以下几点:
use URI::Escape qw( uri_unescape );
my $unescaped_last_item = uri_unescape($escaped_last_item);URI::Escape
请使用use strict; use warnings;!
发布于 2012-02-14 00:07:06
另一个例子是,打开use warnings可以更容易地找出问题所在。
$ perl -Mwarnings -e'$l = "x"; if ($l == "%2B") { print "match\n" }'
Argument "%2B" isn't numeric in numeric eq (==) at -e line 1.
Argument "x" isn't numeric in numeric eq (==) at -e line 1.
matchhttps://stackoverflow.com/questions/9255932
复制相似问题