【发布时间】:2021-05-06 14:19:35
【问题描述】:
我有两个多维数组:array1(@wipr) 和 array2(@widesc)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# Initializing and defining the array
my @wipr = ( ['1111', '2221'],
['1112', '2222'],
['1113', '2223'],
['1115', '2225'],
['1113', '2224'] );
my @widesc = ( ['1111', 'Bug1'],
['1112', 'Feature1'],
['1113', 'Feature2'],
['1114', 'Feature2'] );
# Printing items from the array
#print Dumper(\@wipr);
#print Dumper(\@widesc);
# Using For loop
for(my $m = 0; $m <= $#wipr; $m++)
{
for(my $n = 0; $n <= $#widesc ; $n++)
{
#print $wipr[$m][0], "\n";
#print $widesc[$n][0];
if ( "$wipr[$m][0]" == "$widesc[$n][0]" )
{
print "Matches: $wipr[$m][0] == $widesc[$n][0]";
my $desc="$widesc[$n][1]";
print "desc value is : $desc";
#push(@{$wipr[$m][1][$m]},"$desc");
#print "$wipr[$m][1][$m]";
$wipr[$m][1][$m] = "'$desc'";
#push (@wipr,$wipr[$m][1][$m]);
}
#else
#{
# print "Doesn't Matches: $wipr[$m][0] == $widesc[$n][0]";
#}
}
print "\n";
}
print Dumper(\@wipr);
如果 array1(@wipr) 中第一列的值与 array2(@widesc) 中第一列的值匹配,我们需要将 array2 的第二个元素添加到 array1 中。 如果值不匹配,它应该只输入一个字符串“未找到”
要求的输出应该是:
my @result= ( ['1111', '2221', 'Bug1'],
['1112', '2222', 'Feature1'],
['1113', '2223', 'Feature2'],
['1115', '2225', 'Not Found'],
['1113', '2224', 'Feature2'] );
我在 array1 中添加第三列时遇到了困难。你能帮忙吗?
错误:在arrays1.pl line33 使用“strict refs”时不能使用字符串(“2221”)作为数组引用。
【问题讨论】:
-
push( @{$wipr[$m]}, $desc );或$wipr[$m][2] = $desc; -
我希望将其添加为第三列。我看到的错误是“不能使用字符串(“2221”)作为数组引用,而在 arrays1.pl line33 处使用“严格引用”。
-
第 33 行似乎是:
$wipr[$m][1][$m] = "'$desc'";。@wipr是一个数组。@wipr[$m]是一个数组。$wipr[$m][1]是一个字符串。@wipr[$m][1]不是数组。所以索引它是没有意义的。 -
感谢 jhnc。这是解决的。
# Using For loop for(my $m = 0; $m <= $#wipr; $m++) { for(my $n = 0; $n <= $#widesc ; $n++) { #print $wipr[$m][0], "\n"; #print $widesc[$n][0]; if ( "$wipr[$m][0]" == "$widesc[$n][0]" ) { print "Matches: $wipr[$m][0] == $widesc[$n][0]"; my $desc="$widesc[$n][1]"; print "desc value is : $desc"; #push(@{$wipr[$m][1][$m]},"$desc"); #print "$wipr[$m][1][$m]"; $wipr[$m][1][$m] = "'$desc'"; #push (@wipr,$wipr[$m][1][$m]); }
标签: perl