perl – 为什么eq不能使用我的字符串输入?

前端之家收集整理的这篇文章主要介绍了perl – 为什么eq不能使用我的字符串输入?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
刚刚开始学习Perl,即学习程序流程 – 评估字符串和数字以及使用适当的运算符之间的主要差异.简单的脚本,我在这里让我发疯,因为它是一个超级简单的if if语句,应该在“mike”进入运行并且不起作用.它输出else语句.请帮忙

#!C:\strawberry\perl\bin\perl.exe

use strict;
#use warnings;
#use diagnostics;

print("What is your name please?");
$userName = <STDIN>;


if($userName eq "mike"){
    print("correct answer");
}
else{
    print("Wrong answer");
}

解决方法

当我读到你的问题时,我认为你的字符串与等于的数值有问题.考虑以下情况:

#!/usr/bin/env perl

use strict;
use warnings;

print("What is the meaning of life,the universe and everything? ");
chomp(my $response = <STDIN>);

if ( $response == 42) {
#if ( 42 ~~ $response ) {
    print "correct answer\n";
} else {
    print "Wrong answer\n" ;
}

尝试两个不同的if语句.回答像家人一样的好事,看看会发生什么. ~~是智能匹配运算符,它帮助了Perl中的一些问题.阅读更多关于它here(在“详细的智能匹配”下).另请注意chomp运算符的内联使用.

猜你在找的Perl相关文章