2014-03-27 38 views
2

我想写一个循环,将确定用户输入是否匹配字符串$ rev。无论我为$ seq输入什么内容,它都会返回它不匹配,即使它实际上是。我究竟做错了什么?谢谢你的帮助。Perl - 简单的如果陈述不会工作

$seq = <>; 
$rev = "string"; 
if ($seq eq $rev){ 
    printf("The two strings match.\n"); 
} 
else { 
    printf("The two strings do NOT match.\n"); 
} 
+4

你总是键入一个新行,但不要将其删除。 'chomp'。 – ikegami

+6

如果您不需要'printf'的额外功能,则应该使用'print'。 – TLP

回答

8

您需要chomp您的输入删除换行符:

$seq = <>; 
chomp $seq; 
$rev = "string"; 
if ($seq eq $rev){ 
    print "The two strings match.\n"; 
} 
else { 
    print "The two strings do NOT match.\n"; 
} 

此外,使用print代替printf,因为你没有指定的格式。

1

改变你的代码

$seq = <>; 

chomp($seq = <>);