不能使用字符串作为数组引用的错误

不能使用字符串作为数组引用的错误

想打印一个二维数组,却报这样的错
[root: temp]$ perl array.pl
Can't use string ("MANUAL") as an ARRAY ref while "strict refs" in use at array.pl line 14.

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl

use strict;

my @rc;
my @tmp_row=("t2str","","","No","","","Design","","MANUAL");
push @rc,reverse @tmp_row;

for my $i (0..$#rc)
{
    my $row = $rc[$i];
    for my $j (0..$@{$row})
    {
        print $row->[$j]."\n";
    }
}
~                           

在 perldiag 中搜索下这个错误信息,看看对它们的解释。另外也可以看看 perldoc strict
@rc的元素并不是引用。

[Copy to clipboard] [ - ]
CODE:
for my $i (0..$#rc) {
    print $rc[$i], "\n";
}

push @rc,reverse @tmp_row;

我把数组压进数组,第二个数组不是二维数组了吗?
为什么不是?


QUOTE:
原帖由 dl0622 于 2008-12-3 17:55 发表
push @rc,reverse @tmp_row;

我把数组压进数组,第二个数组不是二维数组了吗?
为什么不是?



[Copy to clipboard] [ - ]
CODE:
push @rc, [reverse @tmp_row]

想知道为什么不可以

QUOTE:
perldoc -f push

还有
或许你想要的是这样的代码

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl
use strict;
use warnings;
my @rc;
my @tmp_row=("t2str","","","No","","","Design","","MANUAL");
push @rc,[reverse @tmp_row];
foreach my $i (0..$#rc) {
    my $row = $rc[$i];
    for my $j (0..$#{$row}) {
        print $row->[$j]."\n";
    }

or

QUOTE:
#!/usr/bin/perl
use strict;
use warnings;
my @rc;
my @tmp_row=("t2str","","","No","","","Design","","MANUAL");
push @rc,[reverse @tmp_row];
foreach (@rc){
        foreach (@{$_}){
                print $_,"\n";
        }
}

push @rc, [reverse @tmp_row]
我发现只是把@tmp_row的倒数第一个值存进@rc,为什么不是整个数组压进去?
压的是整个数组
只不过你最后循环打印的代码还是有问题的
所以打印出来一个
对比一下我给出的代码


QUOTE:
原帖由 dl0622 于 2008-12-3 17:55 发表
push @rc,reverse @tmp_row;

我把数组压进数组,第二个数组不是二维数组了吗?
为什么不是?

要使用引用才行。

[Copy to clipboard] [ - ]
CODE:

#!/usr/bin/perl

use strict;

my @rc;
my @tmp_row=("t2str","","","No","","","Design","","MANUAL");

push @rc, [ (reverse @tmp_row) ];
for my $i (0 .. $#rc) {
    my $row = $rc[$i];

    for my $j (0..$#{$row}) {
        print "$i, $j:", $row->[$j], "\n";
    }
}

print "-" x 50, "\n";

my @rc2;
push @rc2, map { \$_ } reverse @tmp_row;

for my $i (0 .. $#rc2) {

    print "$i:", ${$rc2[$i]}, "\n";
}



[Copy to clipboard] [ - ]
CODE:
$ ./hhhh
0, 0:MANUAL
0, 1:
0, 2:Design
0, 3:
0, 4:
0, 5:No
0, 6:
0, 7:
0, 8:t2str
--------------------------------------------------
0:MANUAL
1:
2:Design
3:
4:
5:No
6:
7:
8:t2str

盾来你用PERL比较久了,从简短代码就可看出,呵,多多指教