我是(一个完整的Perl新手)在if声明中进行字符串比较:
如果我这样做:
if ($str1 == "taste" && $str2 == "waste") { }
Run Code Online (Sandbox Code Playgroud)
我看到了正确的结果(即如果条件匹配,则评估"then"块).但我看到这些警告:
参数"taste"在行号x的数字eq(==)中不是数字.
参数"waste"在行号x的数字eq(==)中不是数字.
但如果我这样做:
if ($str1 eq "taste" && $str2 eq "waste") { }
Run Code Online (Sandbox Code Playgroud)
即使满足if条件,它也不会评估"then"块.
在这里,$str1是taste和$str2是waste.
我该怎么解决这个问题?
我有以下模式,我试图用来匹配信用卡到期日期:
(0[1-9]|1[0-2])\/?(([0-9]{4})|[0-9]{2}$)
Run Code Online (Sandbox Code Playgroud)
我正在测试以下字符串:
02/13
0213
022013
02/2013
02/203
02/2
02/20322
Run Code Online (Sandbox Code Playgroud)
它应该只匹配前四个字符串,最后3个字符串不应该匹配,因为它们无效.但是,当前模式也匹配最后一个字符串.我究竟做错了什么?
当一个人输入一个表示退出或退出的输入时,我试图让我的if语句捕获,但事实并非如此.这是我的代码
use strict;
use warnings;
my $g=0;
my $rn = int(rand(25));
until ($g == $rn) {
$g = <STDIN>;
if ($g == $rn) {
print "You got it";
} elsif (chomp($g) eq "quit" || chomp($g) eq "exit") {
print "it triggered";
} elsif ($g > $rn) {
print "incorrect its lower";
} elsif ($g <$rn) {
print "incorrect its higher";
} else {
print "end";
}
}
}
Run Code Online (Sandbox Code Playgroud)
该
elsif (chomp($g) eq "quit" || chomp($g) eq "exit) {
Run Code Online (Sandbox Code Playgroud)
尽管多次尝试捕捉错误,但是线路并没有捕获.我试过打印出程序看到的东西无济于事.当我输入严格/警告退出时,我得到的回应是
argument …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何比较Perl中的两个字符串?
为什么这个脚本总是返回"你赢了"?
print "Choose heads or tails :\n";
$answer = <STDIN>;
chomp $answer;
if( $answer == "heads" ) {
print "You won\n";
}
else {
print "Moron! You lost.\n"
}
Run Code Online (Sandbox Code Playgroud)
什么应该是相同的正确代码?
我的foreach循环不能按照我的意愿工作.我希望它循环遍历数组,并找到一个元素存在的时间和元素不存在的时间.但是,我得到了所有人的说法Not an apple.
@fruit = ('apple','orange','pear');
foreach(@fruit){
if(@fruit != 'apple'){
print "Not an apple\n";
}
else{
print "Is an apple\n";
}
}
Run Code Online (Sandbox Code Playgroud) print "fruit list\n";
print "1.\tApple\n";
print "2.\tOrange\n";
print "3.\tPic\n";
print "3.\tBanana\n";
print "Based on fruit list above, please key in your favorite fruit name.\n";
%fruit_list = (
1 => 'Apple',
2 => 'Orange',
3 => 'Pic',
4 => 'Banana'
);
$fruit = $fruits[<STDIN>];
if ( $fruit == $fruit_list{'1'} ) {
func1();
}
elsif ( $fruit == $fruit_list{'2'} ) {
func2();
}
sub func1 {
print "executing function 1\n";
}
sub func2 {
print "executing function 2\n";
}
Run Code Online (Sandbox Code Playgroud)
fruit …Run Code Online (Sandbox Code Playgroud) 好吧,我有这行代码,我无法弄清楚,如果有人可以帮助我,这将是伟大的.我正在制作一个地理位置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!";
}
Run Code Online (Sandbox Code Playgroud)
好吧基本上我正在尝试使if语句读取JSON代码并使其在列出特定名称时打印某些文本.我正在向文件添加更多的ISP,但现在只是这两个.
如果有人能用这行代码帮助我,因为我真的无法弄明白.