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\nI|love|perl\n";
print "\n=========================================\n";
print $text;
print "\n=========================================\n";
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] \t";
        }
        print "\n";
    }
}
&display(@array1);
print "\n---------------------------------\n";
&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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM