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

  • 相关阅读:
    SA练习题总结-篇一
    树上距离(树形DP)
    Codeforces Round #633(Div.2) E. Perfect Triples
    Codeforces Round #633 (Div. 2) D.Edge Weight Assignment
    问题 B: FZB(树形DP+边记忆化)
    【Matlab】自学笔记——基础知识篇
    【Python科学计算】Numpy——ndarry
    退役总结
    [树的遍历]树的遍历(PTA)
    [stl]集合相似度(PTA)
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3388832.html
Copyright © 2011-2022 走看看