2

好的,所以我得到了一个数组(AoA),我需要将它传递给一个子例程,然后访问它。这行得通……但它是否严格正确,确实有更好的方法让我这样做吗?

#!/usr/bin/perl
$a = "deep";
push(@b,$a);
push(@c,\@b);

print "c = @{$c[0]}\n";
&test(\@c);

sub test
{
    $d = $_[0];
    print "sub c = @{$$d[0]}\n";
}

谢谢

4

2 回答 2

3

绝对更好的方法是在使用变量use strict;之前use warnings;声明变量。

此外,您知道命名您的 varab- 给它们起有意义的名称并不是一个好习惯,尤其是因为$a变量是由编译器定义的(与 一起使用的sort {} @)。

于 2013-12-07T07:57:00.580 回答
1
use strict;          # Always!
use warnings;        # Always!

sub test {
    my ($d) = @_;
    print "$d->[0][0]\n";

    # Or to print all the deep elements.
    for my $d2 (@$d) {
       for (@$d2) {
          print "$_\n";
       }
    }
}

{
   my $a = "deep";   # or:  my @c = [ [ "deep" ] ];
   my @b;
   my @c;
   push(@b,$a);
   push(@c,\@b);
   test(\@c);        # Can't pass arrays per say, just refs. You had this right.
}

仍然需要更好的名字。特别是应该避免,因为它们会$a干扰.$bsort

于 2013-12-07T16:39:45.947 回答