zoukankan      html  css  js  c++  java
  • perl 二维数组

    perl没有真正的二维数组,所谓的二维数组其实是把一维数组以引用的方式放到另外一个一维数组。

    二维数组定义 :

    my @array1=([1,2],[3,4],[45,9],[66,-5]);               <-----------使用[]表示匿名数组

    或者

    my @array2=qw/this is a array/;
    my @array3=("another","array");
    my @array4=(@array2,@array3);                  <------------使用@表示引用数组

    二维数组的使用

    $array1[1][1]  或者$array1[1]->[1]

    $array1[1] 代表数组的地址

    例子:

    #!/usr/bin/perl -w
    use strict;
    my @array1=([1,2],[3,4],[45,9],[66,-5]);
    print $array1[1][1] ;
    print $array1[1]->[1];
    print $array1[1];
    my @array2=qw/this is a array/;
    my @array3=("another","array");
    my @array4=(@array2,@array3);
    my $text="this|is|a|test
    I|love|perl
    ";
    print "
    =========================================
    ";
    print $text;
    print "
    =========================================
    ";
    sub display
    {
        my @temp=@_;
        for(my $i=0;$i<scalar(@temp);$i++)
        {
            for(my $j=0;$j<scalar(@{$temp[$i]});$j++)
            {
                print "$temp[$i][$j] 	";
            }
            print "
    ";
        }
    }
    &display(@array1);
    print "
    ---------------------------------
    ";
    &display(@array4);

    结果:

    D:perl>perl array.pl

    44ARRAY(0x52e1d8) =========================================

    this|is|a|test I|love|perl

    =========================================

    1       2

    3       4

    45      9

    66      -5

    ---------------------------------

    this    is      a       array

    another         array

  • 相关阅读:
    关于字符串转义的代码
    JAVA发布aar文件
    apache虚拟主机配置HTTPS
    font-face跨域办法
    Javascript数组方法(译)
    Python动态生成变量
    给AOP的after函数使用原函数局部变量
    stopImmediatePropagation的应用
    IE9或以上的浏览器flash值为空时,导致domready不触发
    html写法对gzip压缩率的影响
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3388832.html
Copyright © 2011-2022 走看看