字符串比较总是为真
String comparison is always true
这是我目前拥有的代码。 运行 在 Perl v5.8.9 上。
#!/usr/bin/perl
# Prompts the user for their desired configuration
print "\n";
print "Please type BB for breadboard, or ACU for the Audio Control Unit.\n";
print "Do you want a BB or ACU version of alarm programmer: ";
$a = <STDIN>;
if($a == "ACU"){
print("Initiating alarm programmer for ACU...\n");
}else{
print("Initiating alarm programmer for breadboard...\n");
}
这是一些示例输出:
C:\Users\U157280\Documents\Alarm Programmer>PerlAutonomy.pl
Please type BB for breadboard, or ACU for the Audio Control Unit.
Do you want a BB or ACU version of alarm programmer: ACU
Initiating alarm programmer for ACU...
还有一个:
C:\Users\U157280\Documents\Alarm Programmer>PerlAutonomy.pl
Please type BB for breadboard, or ACU for the Audio Control Unit.
Do you want a BB or ACU version of alarm programmer: BB
Initiating alarm programmer for ACU...
如您所见,它为两者选择了 ACU
,即使这些陈述看起来是正确的。
我做错了什么?
试试这个:
#!/usr/bin/perl
# Prompts the user for their desired configuration
print "\n";
print "Please type BB for breadboard, or ACU for the Audio Control Unit.\n";
print "Do you want a BB or ACU version of alarm programmer: ";
$a = <STDIN>;
chomp($a);
if($a eq "ACU"){
print("Initiating alarm programmer for ACU...\n");
}else{
print("Initiating alarm programmer for breadboard...\n");
}
您有两个问题,首先,=
是赋值而不是比较。其次,您在输入的末尾有一个您没有考虑的换行符。 chomp()
去掉换行符,eq
运算符进行字符串比较。
以下代码片段是 OP 修改后的代码,仅作了极少的更正。
请在您的脚本中包含 use strict;
和 use warnings
,它会警告您有关代码的潜在问题——总之 运行 它将为您省去很多麻烦代码更正。
您应该使用字符串比较运算符 eq
而不是数字比较运算符 ==
#!/usr/bin/perl
#
use strict;
use warnings;
use feature 'say';
# Prompts the user for their desired configuration
say '
Available options:
BB -- for breadboard
ACU -- for the Audio Control Unit
';
print ' Please make a choice: ';
my $choice = <>;
chomp($choice);
if( $choice eq 'ACU') {
say "\n Initiating alarm programmer for ACU...";
} elsif( $choice eq 'BB' ) {
say "\n Initiating alarm programmer for breadboard...";
} else {
say "\n Wrong choice...";
}
exit 0;
要运行外部命令或脚本你应该使用system
参考:
这是我目前拥有的代码。 运行 在 Perl v5.8.9 上。
#!/usr/bin/perl
# Prompts the user for their desired configuration
print "\n";
print "Please type BB for breadboard, or ACU for the Audio Control Unit.\n";
print "Do you want a BB or ACU version of alarm programmer: ";
$a = <STDIN>;
if($a == "ACU"){
print("Initiating alarm programmer for ACU...\n");
}else{
print("Initiating alarm programmer for breadboard...\n");
}
这是一些示例输出:
C:\Users\U157280\Documents\Alarm Programmer>PerlAutonomy.pl
Please type BB for breadboard, or ACU for the Audio Control Unit.
Do you want a BB or ACU version of alarm programmer: ACU
Initiating alarm programmer for ACU...
还有一个:
C:\Users\U157280\Documents\Alarm Programmer>PerlAutonomy.pl
Please type BB for breadboard, or ACU for the Audio Control Unit.
Do you want a BB or ACU version of alarm programmer: BB
Initiating alarm programmer for ACU...
如您所见,它为两者选择了 ACU
,即使这些陈述看起来是正确的。
我做错了什么?
试试这个:
#!/usr/bin/perl
# Prompts the user for their desired configuration
print "\n";
print "Please type BB for breadboard, or ACU for the Audio Control Unit.\n";
print "Do you want a BB or ACU version of alarm programmer: ";
$a = <STDIN>;
chomp($a);
if($a eq "ACU"){
print("Initiating alarm programmer for ACU...\n");
}else{
print("Initiating alarm programmer for breadboard...\n");
}
您有两个问题,首先,=
是赋值而不是比较。其次,您在输入的末尾有一个您没有考虑的换行符。 chomp()
去掉换行符,eq
运算符进行字符串比较。
以下代码片段是 OP 修改后的代码,仅作了极少的更正。
请在您的脚本中包含 use strict;
和 use warnings
,它会警告您有关代码的潜在问题——总之 运行 它将为您省去很多麻烦代码更正。
您应该使用字符串比较运算符 eq
而不是数字比较运算符 ==
#!/usr/bin/perl
#
use strict;
use warnings;
use feature 'say';
# Prompts the user for their desired configuration
say '
Available options:
BB -- for breadboard
ACU -- for the Audio Control Unit
';
print ' Please make a choice: ';
my $choice = <>;
chomp($choice);
if( $choice eq 'ACU') {
say "\n Initiating alarm programmer for ACU...";
} elsif( $choice eq 'BB' ) {
say "\n Initiating alarm programmer for breadboard...";
} else {
say "\n Wrong choice...";
}
exit 0;
要运行外部命令或脚本你应该使用system
参考: