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

原文地址:https://www.cnblogs.com/tobecrazy/p/3388832.html