如果声明失败

时间:2017-08-24 18:06:19

标签: json perl

好的,所以我有这行代码,我无法弄清楚,如果有人可以帮助我,那将是伟大的。我正在制作一个地理位置perl脚本作为我项目的一部分。

这是代码行

$isps = $info->{'isp'};

if ($isps = "Time Warner Cable")
 {

  print "Isp found, go to $website for more information\n";       
}

if ($isps = "Google") {

    print "Isp found, go to $website for more information\n";       
} else {

    print "No ISP located! No way of Contact via this terminal!";
}

好的,所以基本上我试图让if语句读取JSON代码并使其在列出特定名称时打印某些文本。我正在向文件中添加更多的ISP,但现在只是这两个。

如果有人能用这行代码帮助我,因为我真的无法弄明白。

2 个答案:

答案 0 :(得分:5)

=是赋值运算符。您需要字符串比较运算符eq

在此行中,您将字符串"Time Warner Cable"分配给$isps变量。然后if - 条件查看字符串并将其解释为true。下一个条件相同。

if ($isps = "Time Warner Cable")

相反,你想要:

my $isps = $info->{'isp'};

if ($isps eq "Time Warner Cable") {
    print "Isp found, go to $website for more information\n";       
}
elsif ($isps eq "Google") {
    print "Isp found, go to $website for more information\n";       
} else {
    print "No ISP located! No way of Contact via this terminal!\n";
}

答案 1 :(得分:1)

您应该更新您的问题,告诉我们您当前的代码出了什么问题。

一个主要问题是=是赋值运算符。要比较字符串是否相等,请使用eq运算符:

if ($isps eq "Time Warner Cable") # ...

==运算符执行数值比较。)

有关详情perldoc perlop,可在线获取here

相关问题