• 数组的数组


    Perl 免费提供许多数据结构,这些数据结构在其他编程语言里是需要你自己制作的。
    
    比如那些计算机 科学的新芽们都需要学习的堆栈和队列在 Perl 里都只是数组
    
    pop:
    pop 操作将数组的最后一个元素取出并返回:
    
    
    
    Vsftp:/root/perl/5# cat a1.pl 
    my @arr=qw/a b c d e f g/;
    print "@arr is @arr
    ";
    pop @arr;
    print "@arr is @arr
    ";
    
    Vsftp:/root/perl/5# perl a1.pl 
    @arr is a b c d e f g
    @arr is a b c d e f
    
    push,它可以将一个元素(或者一列元素)加在数组的末尾:
    
    Vsftp:/root/perl/5# cat a1.pl 
    my @arr=qw/a b c d e f g/;
    print "@arr is @arr
    ";
    pop @arr;
    print "@arr is @arr
    ";
    
    push (@arr,'xxyy');
    print "@arr is @arr
    ";
    Vsftp:/root/perl/5# perl a1.pl 
    @arr is a b c d e f g
    @arr is a b c d e f
    @arr is a b c d e f xxyy
    
    shift 拿掉数组最左边的一个值
    Vsftp:/root/perl/5# cat a2.pl 
    my @arr=qw/a b c d e f g/;
    print "@arr is @arr
    ";
    shift @arr;
    print "@arr is @arr
    ";
    
    
    Vsftp:/root/perl/5# perl a2.pl 
    @arr is a b c d e f g
    @arr is b c d e f g
    
    
    
    unshift:添加一个值到数组的最左边。
    
    
    Vsftp:/root/perl/5# cat a2.pl 
    my @arr=qw/a b c d e f g/;
    print "@arr is @arr
    ";
    unshift (@arr,'999');
    print "@arr is @arr
    ";
    
    Vsftp:/root/perl/5# perl a2.pl 
    @arr is a b c d e f g
    @arr is 999 a b c d e f g
    
    
    
    9.1 数组的数组
    
    9.1.1 创建和访问一个两维数组
    
    
    Vsftp:/root/perl/5# cat a3.pl 
    @Aoa = (
    ["fred", "barney" ],
    ["george", "jane", "elroy" ],
    ["homer", "marge", "bart" ],
    );
    
    print @Aoa;
    print "
    ";
    Vsftp:/root/perl/5# perl a3.pl 
    ARRAY(0xb55d48)ARRAY(0xb71358)ARRAY(0xb71538)
    
    
    Vsftp:/root/perl/5# cat a3.pl 
    @Aoa = (
    ["fred", "barney" ],
    ["george", "jane", "elroy" ],
    ["homer", "marge", "bart" ],
    );
    
    print @{$Aoa[0]};
    print "
    ";
    print $Aoa[0]->[1];
    print "
    ";
    
    
    Vsftp:/root/perl/5# perl a3.pl 
    fredbarney
    barney
    
    
    # 创建一个指向一个数组的数组的引用。
    
    
    Vsftp:/root/perl/5# cat a4.pl 
    $ref_to_AoA = [
    [ "fred", "barney", "pebbles", "bamm bamm", "dino", ],
    [ "homer", "bart", "marge", "maggie", ],
    [ "george", "jane", "elroy", "judy", ],
    ];
    
    print $ref_to_AoA;
    print "
    ";
    print @{$ref_to_AoA};
    print "
    ";
    print $ref_to_AoA->[0]->[3];
    print "
    ";
    Vsftp:/root/perl/5# perl a4.pl 
    ARRAY(0x104c638)
    ARRAY(0x1029d48)ARRAY(0x10455e0)ARRAY(0x1045670)
    bamm bamm
    
    
    请记住在每一对相邻的花括弧或方括弧之间有一个隐含的 ->
    
    
    9.1.2 自行生长
    
    Vsftp:/root/perl/5# cat aa
    1   4 7
    a1 a4 a7
    z1 a6 z9
    
    Vsftp:/root/perl/5# cat a5.pl 
    open (A,"aa");
    while (<A>){
       chomp $_;
       my @tmp = split(/s+/,$_);
       push @AOA ,[@tmp];
       };
    print @AOA;
    print "
    ";
    print $AOA[1]->[1];
    print "
    ";
    
    Vsftp:/root/perl/5# perl a5.pl 
    ARRAY(0xc02d48)ARRAY(0xc1e340)ARRAY(0xc1e5e0)
    a4
    
    
    
    /*****************************
    
    Vsftp:/root/perl/5# perl a6.pl 
    Type of arg 1 to push must be array (not scalar dereference) at a6.pl line 6, near "];"
    Execution of a6.pl aborted due to compilation errors.
    Vsftp:/root/perl/5# cat a6.pl 
    open (A,"aa");
    my @ref_to_AOA=@AOA;
    while (<A>){
       chomp $_;
       my @tmp = split(/s+/,$_);
       push $ref_to_AOA ,[@tmp];
       };
    print @AOA;
    print "
    ";
    print $AOA[1]->[1];
    print "
    ";
    
    Vsftp:/root/perl/5# perl a6.pl 
    Type of arg 1 to push must be array (not scalar dereference) at a6.pl line 6, near "];"
    Execution of a6.pl aborted due to compilation errors.
    
    
    
    Vsftp:/root/perl/5# cat a7.pl 
    sub func() {
        my $a=shift;
        my $b=shift;
        return $a + $b;
       };
    
    
    for $x (0..9){ # 对每一行...
      for $y (0..9) { # 对每一列...
      $AoA[$x][$y] = &func($x, $y); # ...设置调用
      }
    };
    
    
    print  @AoA;
    print "
    ";
    print @{$AoA[0]};
    print "
    ";
    print @{$AoA[1]};
    print "
    ";
    print @{$AoA[2]};
    print "
    ";
    Vsftp:/root/perl/5# perl a7.pl 
    ARRAY(0x692358)ARRAY(0x69e290)ARRAY(0x69e3b0)ARRAY(0x69e4d0)ARRAY(0x69e5f0)ARRAY(0x69e710)ARRAY(0x69e830)ARRAY(0x69e950)ARRAY(0x69ea70)ARRAY(0x69eb90)
    0123456789
    12345678910
    234567891011
    
    这种方式的访问:
    $AoA[$x][$y]
    
    $AoA[$x]->[$y]
    
    
    Vsftp:/root/perl/5# cat a8.pl 
    my @AoA=(1,2,3,[a1,a2,a3]);
    print $AoA[3][2];
    print "
    ";
    print $AoA[3]->[2];
    print "
    ";
    Vsftp:/root/perl/5# 
    Vsftp:/root/perl/5# perl a8.pl 
    a3
    a3
    
    
    
    Vsftp:/root/perl/5# cat a9.pl 
    use Data::Dumper;
    my $ref_to_AoA=[(1,f2,f3),(a1,a2,a3),(z1,z2,z3)];
    print Dumper($ref_to_AoA);
    print "
    ";
    print $ref_to_AoA->[0]."
    ";
    print $ref_to_AoA->[1]."
    ";
    print $ref_to_AoA->[2]."
    ";
    print $ref_to_AoA->[3]."
    ";
    print $ref_to_AoA->[4]."
    ";
    print $ref_to_AoA->[5]."
    ";
    print $ref_to_AoA->[6]."
    ";
    
    Vsftp:/root/perl/5# perl a9.pl 
    $VAR1 = [
              1,
              'f2',
              'f3',
              'a1',
              'a2',
              'a3',
              'z1',
              'z2',
              'z3'
            ];
    
    1
    f2
    f3
    a1
    a2
    a3
    z1
    
    
    
    Vsftp:/root/perl/5# cat a10.pl 
    use Data::Dumper;
    my $ref_to_AoA=[[1,f2,f3],[a1,a2,a3],[z1,z2,z3]];
    print Dumper($ref_to_AoA);
    print "
    ";
    print $ref_to_AoA->[0]."
    ";
    print $ref_to_AoA->[1]."
    ";
    print $ref_to_AoA->[2]."
    ";
    print  $ref_to_AoA->[0]->[2]."
    ";
    Vsftp:/root/perl/5# perl a10.pl 
    $VAR1 = [
              [
                1,
                'f2',
                'f3'
              ],
              [
                'a1',
                'a2',
                'a3'
              ],
              [
                'z1',
                'z2',
                'z3'
              ]
            ];
    
    ARRAY(0x1742d48)
    ARRAY(0x1796628)
    ARRAY(0x178aa00)
    f3
    
    
    
    /****************************
    Vsftp:/root/perl/5# cat a11.pl 
    use Data::Dumper;
    my $ref_to_AoA=[[1,f2,f3],[a1,a2,a3],[z1,z2,z3],[z1,z2,dsd3],[z1,z2,zdas3],[z1,z2,zda3]];
    print Dumper($ref_to_AoA);
    print "
    ";
    print  $ref_to_AoA->[0]->[2]."
    ";
    print  $ref_to_AoA->[1]->[2]."
    ";
    print  $ref_to_AoA->[2]->[2]."
    ";
    print  $ref_to_AoA->[3]->[2]."
    ";
    print  $ref_to_AoA->[4]->[2]."
    ";
    print  $ref_to_AoA->[5]->[2]."
    ";
    for $x (0..5) { # 对每一行...
    $ref_to_AoA->[$x][2] = &func($x); # ...设置第四行
    };
    print  $ref_to_AoA->[0]->[2]."
    ";
    print  $ref_to_AoA->[1]->[2]."
    ";
    print  $ref_to_AoA->[2]->[2]."
    ";
    print  $ref_to_AoA->[3]->[2]."
    ";
    print  $ref_to_AoA->[4]->[2]."
    ";
    print  $ref_to_AoA->[5]->[2]."
    ";
    sub func() {
        my $a=shift;
        my $b=shift;
        return $a + $b;
       };
    
    Vsftp:/root/perl/5# perl a11.pl 
    
    
    f3
    a3
    z3
    dsd3
    zdas3
    zda3
    0
    1
    2
    3
    4
    5
    
    
    Vsftp:/root/perl/5# cat a12.pl 
    use Data::Dumper;
    my @AoA=([1,f2,f3],[a1,a2,a3],[z1,z2,z3],[z1,z2,dsd3],[z1,z2,zdas3],[z1,z2,zda3]);
    print @{ $AoA[0] };
    print "
    ";
    push @{ $AoA[0] }, "wilma", "betty";
    
    print @{ $AoA[0] };
    print "
    ";
    Vsftp:/root/perl/5# perl a12.pl 
    1f2f3
    1f2f3wilmabetty
    
    
    因为给 push 的参数必须是一个真正的数组,而不只是一个指向 一个数组的引
    用。因此,第一个参数绝对必须以 @ 字符开头。而跟在 @ 后面的东西则可以忽略一些。
    
    
    9.1.3  访问的和打印
    
    现在把数据结构打印出来,如果你只想要一个元素,下面的就足够了:
    
    Vsftp:/root/perl/5# cat a13.pl 
    my @AoA=(1,2,3,[a1,a2,a3,a4]);
    print $AoA[3][2];
    print "
    ";
    Vsftp:/root/perl/5# perl a13.pl 
    a3
    
    Vsftp:/root/perl/5# cat a13.pl 
    my @AoA=(1,2,3,[a1,a2,a3,a4]);
    print @AoA;
    print "
    ";
    Vsftp:/root/perl/5# perl a13.pl 
    123ARRAY(0x2161d48)
    
    
    Vsftp:/root/perl/5# cat a13.pl 
    my @AoA=([1],[2],[3],[a1,a2,a3,a4]);
    print @AoA;
    print "
    ";
    for $row (@AoA) {
    print "@$row
    ";
    }
    Vsftp:/root/perl/5# perl a13.pl 
    ARRAY(0x144fd48)ARRAY(0x146b340)ARRAY(0x146b550)ARRAY(0x146b598)
    1
    2
    3
    a1 a2 a3 a4
    
    
    
    Vsftp:/root/perl/5# cat a13.pl 
    my @AoA=([1],[2],[3],[a1,a2,a3,a4]);
    for $i (0..$#AoA) {
    print "row $i is: @{$AoA[$i]}
    ";
    }
    Vsftp:/root/perl/5# vim a13.pl 
    Vsftp:/root/perl/5# cat a13.pl 
    my @AoA=([1],[2],[3],[a1,a2,a3,a4]);
    for $i (0..$#AoA) {
    print "row $i is: @{$AoA[$i]}
    ";
    }
    Vsftp:/root/perl/5# perl a13.pl 
    row 0 is: 1
    row 1 is: 2
    row 2 is: 3
    row 3 is: a1 a2 a3 a4
    
    
    9.1.4 片段
    
    取出片段:
    Vsftp:/root/perl/5# cat a14.pl 
    my @AoA=([1,2,3],['aA','ADS','DSD'],['DAD',323,233],[232,'FF','344G'],['sas21','121','sad1231','asda1231','3131','sad213','adda','ewds']);
    print @AoA;
    print "
    ";
    print $AoA[4]->[2];
    print "
    ";
    
    @part = ();
    for ($y =3 ; $y < 8; $y++) {
    push @part, $AoA[4][$y];
    }
    print @part;
    print "
    ";
    
    
    Vsftp:/root/perl/5# perl a14.pl 
    ARRAY(0x900d48)ARRAY(0x927638)ARRAY(0x9276b0)ARRAY(0x927728)ARRAY(0x9277a0)
    sad1231
    asda12313131sad213addaewds
    Vsftp:/root/perl/5# 
    
    
    
    9.1.5  常见错误:
    
    Vsftp:/root/perl/5# perl a16.pl 
    ARRAY(0x1933d48) ARRAY(0x194f358) ARRAY(0x194f538)
    Vsftp:/root/perl/5# cat a16.pl 
    @AoA = ([2, 3], [4, 5, 7], [0] );
    print "@AoA";
    print "
    ";
    
    Vsftp:/root/perl/5# perl a16.pl 
    ARRAY(0x9f4d48) ARRAY(0xa10358) ARRAY(0xa10538)
    
    
    
    Vsftp:/root/perl/5# cat a17.pl 
    sub somefunc() {
         my $a=shift;
         return $a=3;
    };
    
    for $i (1..10) {
    @array = &somefunc($i);
    $AoA[$i] = [@array]; # 把数组引用给到对应的数组元素
    };
    print @AoA;
    print "
    ";
    Vsftp:/root/perl/5# perl a17.pl 
    ARRAY(0x2547f40)ARRAY(0x2563358)ARRAY(0x2563628)ARRAY(0x2563670)ARRAY(0x25636b8)ARRAY(0x256c038)ARRAY(0x256c080)ARRAY(0x256c0c8)ARRAY(0x256c110)ARRAY(0x256c158)

  • 相关阅读:
    第六周作业
    第四周作业(登录验证码)
    JSP第二次作业
    JSP第一次作业求1-100间的素数
    软件测试课堂作业
    安卓作业(购物商城)
    安卓作业
    Android 简易计算器
    jsp第七次作业
    JSP第六次作业
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350024.html
Copyright © 2020-2023  润新知