Perl错误:参数为';数组或哈希查找中的t数字

Perl错误:参数为';数组或哈希查找中的t数字,perl,Perl,我正在编写一个简单的程序,将单词与正则表达式模式进行匹配。但我一直收到上面的错误。这是我的代码: my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'"); foreach my $index (@words) { if ($words[$index] =~ m/ord/) { print "match\n"; } else {print "no match\n";} } 我

我正在编写一个简单的程序,将单词与正则表达式模式进行匹配。但我一直收到上面的错误。这是我的代码:

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");
foreach my $index (@words) {
    if ($words[$index] =~ m/ord/) {
        print "match\n";
    } else {print "no match\n";}
}
我收到的错误:

Argument "Ordinary" isn't numeric in array or hash lookup at test.pl line  6.
Argument "order" isn't numeric in array or hash lookup at test.pl line 6.
Argument "afford" isn't numeric in array or hash lookup at test.pl line 6.
Argument "cordford" isn't numeric in array or hash lookup at test.pl line 6.
Argument "'ORD airport'" isn't numeric in array or hash lookup at test.pl line 6.
no matchno matchno matchno matchno match
有人能给我解释一下错误的原因吗?

这是数组查找

$words[$index];
如果它是一个杂烩,它将是

$words{$index};
数组需要整数索引,但使用的字符串与整数完全不同

如果在Perl中迭代数组,则不需要索引

#!/usr/bin/perl
use strict;
use warnings;

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");
foreach my $word (@words) {
  if($word =~ m/ord/) {      
    print "$word match\n";
  } else {
    print "$word no match\n";
  }
}
注意。我使用了
foreach
,因为您可以看到更多的语言,您也可以使用
for

你也可以尝试一些替代品,注意这不会结束,但值得学习ie

#!/usr/bin/perl
use strict;
use warnings;
my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");
my $iterator = sub {
  my $item = shift(@words);
  push(@words, $item);
  return $item;
};

while(my $item = $iterator->()) {
  print("$item\n");
}
我非常喜欢Perl。

这是数组查找

$words[$index];
如果它是一个杂烩,它将是

$words{$index};
数组需要整数索引,但使用的字符串与整数完全不同

如果在Perl中迭代数组,则不需要索引

#!/usr/bin/perl
use strict;
use warnings;

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");
foreach my $word (@words) {
  if($word =~ m/ord/) {      
    print "$word match\n";
  } else {
    print "$word no match\n";
  }
}
注意。我使用了
foreach
,因为您可以看到更多的语言,您也可以使用
for

你也可以尝试一些替代品,注意这不会结束,但值得学习ie

#!/usr/bin/perl
use strict;
use warnings;
my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");
my $iterator = sub {
  my $item = shift(@words);
  push(@words, $item);
  return $item;
};

while(my $item = $iterator->()) {
  print("$item\n");
}

我非常喜欢Perl。

原因是您的
$index
将生成数组的元素,而不是索引值

现在,
$index
将在每次迭代中生成数组的索引

use strict; 
use warnings;

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");

foreach my $index (0..$#words) {

    if ($words[$index] =~ m/ord/) {

       print "match\n";
    } 

    else {print "no match\n";}
 }
否则。只需使用
$index
检查条件即可

use strict; 

use warnings;

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");

foreach my $index (@words) {

    if ($index =~ m/ord/) {

    print "match\n";

    }

    else {print "no match\n";}
}

原因是
$index
将生成数组的元素,而不是索引值

现在,
$index
将在每次迭代中生成数组的索引

use strict; 
use warnings;

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");

foreach my $index (0..$#words) {

    if ($words[$index] =~ m/ord/) {

       print "match\n";
    } 

    else {print "no match\n";}
 }
否则。只需使用
$index
检查条件即可

use strict; 

use warnings;

my @words = ("Ordinary", "order", "afford", "cordford", "'ORD airport'");

foreach my $index (@words) {

    if ($index =~ m/ord/) {

    print "match\n";

    }

    else {print "no match\n";}
}

这是您展示的代码(稍加改进)

for
循环将
$index
设置为
@words
数组中的每个值。因此,例如,第一次执行循环时,
$index
将设置为
普通
;第二次它将被设置为
order

将其命名为
$index
清楚地表明您希望它包含
@words
的所有索引。你可以这样做

for my $index ( 0 .. $#words ) { ... }
如果你做了这样的改变,你的程序就会运行良好。输出是

不匹配
比赛
比赛
比赛
没有对手
但你从一开始就有了正确的想法。通常情况下,数组只是一个值列表,索引没有相关性。这适用于你的情况,你可以写

for my $word ( @words ) {

    if ( $word =~ m/ord/ ) {
        print "match\n";
    }
    else {
        print "no match\n";
    }
}
或者使用Perl的默认变量
$\uuz
可以编写

for ( @words ) {

    if ( m/ord/ ) {
        print "match\n";
    }
    else {
        print "no match\n";
    }
}
甚至只是

print /ord/ ? "match\n" : "no match\n" for @words;

上面的每个示例都完全相同,因此产生相同的输出

这是您显示的代码(稍微改进)

for
循环将
$index
设置为
@words
数组中的每个值。因此,例如,第一次执行循环时,
$index
将设置为
普通
;第二次它将被设置为
order

将其命名为
$index
清楚地表明您希望它包含
@words
的所有索引。你可以这样做

for my $index ( 0 .. $#words ) { ... }
如果你做了这样的改变,你的程序就会运行良好。输出是

不匹配
比赛
比赛
比赛
没有对手
但你从一开始就有了正确的想法。通常情况下,数组只是一个值列表,索引没有相关性。这适用于你的情况,你可以写

for my $word ( @words ) {

    if ( $word =~ m/ord/ ) {
        print "match\n";
    }
    else {
        print "no match\n";
    }
}
或者使用Perl的默认变量
$\uuz
可以编写

for ( @words ) {

    if ( m/ord/ ) {
        print "match\n";
    }
    else {
        print "no match\n";
    }
}
甚至只是

print /ord/ ? "match\n" : "no match\n" for @words;

上面的每个示例都是完全等效的,因此产生相同的输出

word
是一个数组而不是散列。我认为您没有在这里解决OP的困惑。没有人提到哈希这个错误提到哈希。我认为OP意识到[]和{}在Perl中是根本不同的东西是相关的。
word
是一个数组而不是散列。我认为您没有在这里解决OP的困惑。没有人提到哈希这个错误提到哈希。我认为OP意识到[]和{}在Perl中是根本不同的东西是相关的。在最近的Perl中,您可以对我的$index(keys@words)执行
,而不是使用
0..$#words
@ysth:我不确定这会有什么帮助。它长一两个字符,大多数Perl程序员都不熟悉,而且从v5.12开始只能在Perl上使用,因此存在可移植性问题,我没有意识到这是可能的。现在我知道了,这很有趣,但我不认为我会使用它:)我想如果它以不同的方式处理“稀疏”数组可能会有用吗?在最近的perls中,你可以为我的$index(keys@words)
而不是使用
0..$\words
@ysth:我不确定这会有什么帮助。它长一两个字符,大多数Perl程序员都不熟悉,而且从v5.12开始只能在Perl上使用,因此存在可移植性问题,我没有意识到这是可能的。现在我知道了,这很有趣,但我不认为我会使用它:)我想如果它以不同的方式处理“稀疏”数组,它可能会有用吗?