Perl Two-dimensional arrays

Source: Internet
Author: User



"Reprint" Source: http://www.cnblogs.com/visayafan/


    • 1 Arrays and references
    • 2 Differences of the statements
    • 3 Differences in Access
    • 4 Adding row elements
    • 5 Adding column elements
    • 6 Accessing and printing
      • 6.1 Operator Precedence
      • 6.2 Visit A
      • 6.3 Traversal
    • 7 slices
1 arrays and references


This refers to pointers in C equivalent.
The first column of a two-dimensional array stores a reference to a one-dimensional array of that row instead of storing the concrete element.


2 Differences of the statements
    • The array is declared in the following form:
      Add @ before the array name, followed by ().


my @AoA = (
           [ "fred", "barney", "pebbles", "bambam", "dino", ],
           [ "george", "jane", "elroy", "judy", ],
           [ "homer", "bart", "marge", "maggie", ],
          );
    • The following form of declaration is quoted:
      Use $ before the reference name, followed by [].


$ref_to_AoA = [
               [ "fred", "barney", "pebbles", "bambam", "dino", ],
               [ "george", "jane", "elroy", "judy", ],
               [ "homer", "bart", "marge", "maggie", ],
              ];
3 differences in Access
    • Array access


$AoA [$i] [$j]


Because the first column array contains references, you can also access this:





$AoA [$i]->[$j]
    • Reference access


$ref _aoa->[$i [$j]


Similarly, references can be accessed in this way:





$ref _aoa->[$i]->[$j]
4 adding row elements




My (@AoA, $ref_to_AoA);

Sub print_AoA{
     For (@AoA) {
         Print "@{$_}\n";
     }
     Print "\n";
}

# 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]; # Because the first column of the array AoA needs a reference, and @tmp is an array, the direct assignment will be wrong. [] means to return a reference to @tmp, that is, push the reference of @tmp to the last line of @AoA, and increase the number of rows of the two-dimensional array by 1.
print_AoA();

Push @AoA, @tmp;
print_AoA();


Overwrite rows


#$AoA[0] = @tmp; #$AoA[0] is a scalar type, and @tmp is a list type, so by default the number of tmp is assigned to $AoA[0], ie $AoA[0]= 4;
$AoA[0] = [@tmp]; #overwirte
print_AoA();
5 Adding column elements




Push @{$AoA [0]}, "Wilma", "Betty";


Omit @{}


Use v5.14; # needed for implicit deref of array refs by array ops
Push $AoA[0], "wilma", "betty"; # Cannot pass before version 5.14, because the first parameter of push must be an array. In the new version, there is a reference in $AoA[0], but it is incorrect when there is no reference.
print_AoA();
My $aref = undef;
#push $aref, qw/some value/; # ERROR: Not an ARRAY reference
My $aref = [@tmp];
Push $aref, qw/some value/; # correct, because aref is not a null reference at this time
Print "$aref : @$aref\n";
6 Accessing and printing6.1 Operator Precedence




[Email protected]*%&
6.2 Visit a




Print $AoA [$i] [$j];p rint ref_$aoa->[$i]->[$j];
6.3 Traversal
    • The simplest kind of


For $aref ( @AoA ) # $aref is just a reference in the first column. To access the entire line, you must add @, and the $ access level is higher than @, so () can be omitted.
{
     Say "\t [ @$aref ],";
}
    • Using $#


For my $i (0: $ #AoA) {    say "Elt $i is @{$AoA [$i]}";}
    • Inline loops


for my $i (0 .. $#AoA){
    for my $j (0 .. $#{$AoA[$i]}){
        say "elt $i, $j is $AoA[$i][$j]\n";
    }
}
7 slices


To access several rows of several columns of elements. and MATLAB to access the matrix in the same way.


    • Cut single-row multiple columns


My @part = ();
My $x = 4;
For (my $y = 1; $y<4; $y++){
     Push @part, $AoA[$x][$y];
}  @part = @{$AoA[4]}[1..4];
    • Cut multi-row multiple columns


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];
     }
}

#a simple cycle
For (my $x = 1; $x<=5; $x++){
     Push @newAoA, [@{$AoA[i]}[2 .. 4]];
}
    • Writing functions


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



Perl Two-dimensional arrays


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.