my @AoA = (
           [ "fred", "barney", "pebbles", "bambam", "dino", ],
           [ "george", "jane", "elroy", "judy", ],
           [ "homer", "bart", "marge", "maggie", ],
  • 引用如下形式声明:
    引用名前用$,之后用[]。
    $ref_to_AoA = [
                   [ "fred", "barney", "pebbles", "bambam", "dino", ],
                   [ "george", "jane", "elroy", "judy", ],
                   [ "homer", "bart", "marge", "maggie", ],
    # assign to our array, an array of array references
    @AoA = (
               [ "fred", "barney", "pebbles", "bambam", "dino", ],
               [ "george", "jane", "elroy", "judy", ],
               [ "homer", "bart", "marge", "maggie", ],
    say $AoA[2][1];
    $ref_to_AoA = [
                   [ "fred", "barney", "pebbles", "bambam", "dino", ],
                   [ "george", "jane", "elroy", "judy", ],
                   [ "homer", "bart", "marge", "maggie", ],
    print_AoA();
    my @tmp = (1, 2, 3, 4);
    push @AoA, [@tmp];              # 因为数组AoA第一列需要的是引用,而@tmp是数组,直接赋值会出错。[]表示返回@tmp的引用,即把@tmp的引用push到@AoA最后一行,二维数组行数加1.
    print_AoA();
    push @AoA, @tmp;
    print_AoA();
    
    #$AoA[0] = @tmp;   #$AoA[0]是scalar型,而@tmp是list型,所以用默认把tmp的个数赋给$AoA[0],即$AoA[0]=4;
    $AoA[0] = [@tmp];  #overwirte
    print_AoA();
    
    use v5.14;   # needed for implicit deref of array refs by array ops
    push $AoA[0], "wilma", "betty";          # 在5.14版本之前不能通过,因为以前规定push的第一参数必须为数组。在新版本中当$AoA[0]中存在引用时可以通过,但无引用时不正确。
    print_AoA();
    my $aref = undef;
    #push $aref, qw/some value/;     # 出错:Not an ARRAY reference
    my $aref = [@tmp];
    push $aref, qw/some value/;     # 正确,因为aref此时不是个空引用
    print "$aref : @$aref\n";
    
    for $aref ( @AoA )                #  $aref只是第一列里面的引用,要想访问整行必须加@,又$访问级别比@高,所以()可以省略。
        say "\t [ @$aref ],";
    
    for my $i (0 .. $#AoA){
        say "elt $i is @{$AoA[$i]}";
    
    for my $i (0 .. $#AoA){
        for my $j (0 .. $#{$AoA[$i]}){
            say "elt $i, $j is $AoA[$i][$j]\n";
    
    my @newAoA = ();
    for (my $startx= my $i = 1; $i<=5; $i++){
        for(my $starty = my $j = 2; $j<=4; $j++){
            $newAoA[$i - $startx][$j - $starty] = $AoA[$i][$j];
    #一个循环简单写法
    for (my $x = 1; $x<=5; $x++){
        push @newAoA, [@{$AoA[i]}[2 .. 4]];
    
    sub splice_2D{
        my $lrr = shift;
        my($x_l, $x_h,
           $y_l, $y_h) = @_;
        return map(
                   [ @{$lrr -> [$_]} {$y_l .. $y_h}]
                  )$x_l .. $x_h;
    @newAoA = splice_2D(\@AoA, 1=>5, 2=>4);
    

    Author: visaya fan <visayafan[AT]gmail.com or visayafan[AT]163.com>

    Date: 2011-10-29 15:00:34

    HTML generated by org-mode 6.33x in emacs 23

  •