perl将数组元素与单个字符进行比较

时间:2015-01-11 20:38:34

标签: arrays perl compare

,将数组的所有元素与单个字符进行比较。

chomp($letter=<STDIN>);

 $i=0;

 for($i=0;$i<$length:$i++){
    if(@selectword[$i]==$letter)
           {
            print("Match\n");
            }
           else
            {
            print("No Match\n");
            }
        }
 }

目前无法使用数组设置为test 输出

match
match
match
match

无论输入任何字母。

2 个答案:

答案 0 :(得分:2)

正如我所说,你发布的Perl距离编译还有很长的路要走,但看起来你可能想要这样的东西

use strict;
use warnings;

my @selectword = (); # Initialise with real data

chomp (my $letter = <>);

for my $i (0 .. $#selectword) {
    if ($selectword[$i] eq $letter) {
        print("Match\n");
    }
    else {
      print("No Match\n");
    }
}

答案 1 :(得分:0)

我认为你还没有粘贴整个程序。 但是看一下代码的一部分,你应该使用字符串相等运算符来比较字母&#34; eq&#34;如果(@selectword [$ i] == $ letter)而不是&#34; ==&#34;这是用于数字比较。