Skip to content

Latest commit

 

History

History
executable file
·
822 lines (553 loc) · 19.9 KB

perllol.pod

File metadata and controls

executable file
·
822 lines (553 loc) · 19.9 KB

NAME

perllol - Perl ��ч�������勰�����������篏�������

DESCRIPTION

Declaration and Access of Arrays of Arrays

(��������勰�������勐��臂í����≪�壔�祉��)

Perl ��ф��膀�������筝���ɾ亜�����ŝ����若�炊�������壔����������勰�����(��˨�吾�ャ�≪�˨� ��ŝ�鴻����勉�ŝ�鴻����������若�違����������������������障��)��с����� ��������夌��茹c����������������������⓾�����茲������ŝ����若�炊��������絲障����⓾�� ��ñ����������������勉�с����������勉�с�����

��������勰�������壔�������ŝ�������������育��絽吾�勐𡉴�������� @AoA ��勉�������ŝ����勉�с����� ��������� $AoA[3][2] ��勉�������˨��篋���ゃ�勖兄���絖���ц��膣�������緇������������������с����障����� ��������勐㐂荐���勌�������������障�����������

use 5.010;  # so we can use say()
# ��������̹�������吾�勉�ŝ����<�㋘�潟�鴻�勰��������篁e�ャ�����
@AoA = (
           [ "fred", "barney", "pebbles", "bambam", "dino", ],
           [ "george", "jane", "elroy", "judy", ],
           [ "homer", "bart", "marge", "maggie", ],
);
say $AoA[2][1];
  bart

�����勉��������紊���眼�勖�㋚磁���筝御�㋚磁��с����c�����������羈���������鴻����с����� ��������壔��筝���勌����с��@��������˩撮��ャ�������勉�т幻��㋚磁���篏帥��綽�荀���������c����������ŝ�勉�с����� ������ @AoA ��с�壔�ŝ����⓾�������˨�ŝ����<�㋘�潟�鴻��篁e�ャ����������c�������������勉�с�������違�� 罨<�勉����������吾�������������с����障��:

# ��������吾�勉�ŝ����<�㋘�潟�鴻�勰�������吾�勉�ŝ����<�㋘�潟�鴻��篁e�ャ�����
$ref_to_AoA = [
        [ "fred", "barney", "pebbles", "bambam", "dino", ],
        [ "george", "jane", "elroy", "judy", ],
        [ "homer", "bart", "marge", "maggie", ],
];
say $ref_to_AoA->[2][1];
  bart

紊���眼�勖�㋚磁���紊������c����������������≪�壔�祉�鴻�勖��������紊������c�⓾���������������������� 羈◐�勉����⓾�������������� ��������� C �����奝����c�⓾��Perl ��с�奝�������������с�������ɾ�宴�˩困�����с����ŝ����������с����� $ref_to_AoA ��奝�������吾�勐����с�с����� �����勰�������� @AoA ��с�������������障����������с����� ���罕���˨��$AoA[2] ��奝�������с�壔�ŝ����������吾�勐����с�с����� ��с��������:

$AoA[2][2]
$ref_to_AoA->[2][2]

��������壔��篁ヤ����勉�������ʋ�吾����鴻�с����������������˨�ŝ����障��:

$AoA[2]->[2]
$ref_to_AoA->[2]->[2]

�����勤�������奝�c�������c�������c��(���������紊с����c��������������筝㏍����c��������������) ��������勉����勉�ŝ�勉�с�������у����������������√�違����ɾ�宴�̥����ャ�с����障����� ��������í��筝���ʋ�������˨�������√�違�������壔�������������ŝ����<�㋘�潟�鴻��篆���������� ��鴻�˨�í�с�������������̥����ャ�������������壔�с����障�������� ��������� $ref_to_AoA ���絽吾��綽�荀���������������勉�с�����

Growing Your Own

(紊с�����������)

��阪�������ŝ����若�炊�������勐㐂荐���虁�壔����勉�с�������������勐�眼�ф�違�����荀�膣���� 菴遵�������������������������������壠�������� 0 ������篏����筝������������������˨�壔�í����������勉�с��������?

��障����������˨�������<�ゃ�˨�����茯㏍�粋昭������������荀���⓾�帥�障����������� ��������壚��綺⓾�˩����ゃ�勤�����菴遵�������⓾�������������ŝ����勉�с����� 腱������<�壔�������с��茯㏍�粋昭�����с����������<�ゃ�˨�����筝�茵�(line)���筝���ゃ�勤��(row)�� 絲上��������������茯����荀�膣����絲上�������⓾�������������ŝ����í�������ŝ����<�ゃ�˨�с������� 篁勐�������⓾����障����� ������������ @AoA ��˨����������c����í��荐㊤����������������������勉�с�������違�� ��������壚札筝���勉�������ŝ�������鴻�˨�ŝ����障��:

while (<>) {
        @tmp = split;
        push @AoA, [ @tmp ];
}

��∽�違��篏帥�c�☀˛��粋昭��������������с����障��:

for $i ( 1 .. 10 ) {
        $AoA[$i] = [ somefunc($i) ];
}

�����������壔����������̬┃絎���������������˩戎���筝����紊���違��篏帥�������������с����障�����

for $i ( 1 .. 10 ) {
        @tmp = somefunc($i);
        $AoA[$i] = [ @tmp ];
}

��������吾�勐����с�勉�潟�潟�鴻����í�壔�帥�с����� [ ] ���篏帥��������������荀���с����� 罨<�勉����������吾����⓾����障����勉�壔����⓾����障�������������������с��:

$AoA[$i] = @tmp;   # WRONG!

�����ŝ�������������с�������������������ŝ�������宴�壔����鴻�˨�í��絲障�������������ɱ�����篁������������ 篁e�ャ�壔�������������鴻�˨�í�潟�潟����㏍�鴻����������〠�宴�������������壠����� @tmp ��勤��膣���勖�違�� ��違�������������˨�ŝ����������с�����

use strict ��勐����у��茵���������勉�с�������違��(��������⓾����������⓾����ŝ����勉�ŝ����� ��í�������⓾����⓾����ŝ�����?) 篁ヤ����勖����˨�<����c��絎h�����篁������������������������с��������:

use strict;
my(@AoA, @tmp);
while (<>) {
        @tmp = split;
        push @AoA, [ @tmp ];
}

�����<��������筝���������ʒ����������ŝ�������違�ŝ����ŝ����������������勉�с�壔�������障�����:

while (<>) {
        push @AoA, [ split ];
}

��障�����push() ���篏帥����ŝ����⓾����с����障����� ��í�������若��莨若�������������◑����������������������c�⓾�������勉�ŝ�������贋・ 篁e�ャ�������������������с����障��:

my (@AoA, $i, $line);
for $i ( 0 .. 10 ) {
        $line = <>;
        $AoA[$i] = [ split " ", $line ];
}

�����������壔�����������蘂���˨����с����障��:

my (@AoA, $i);
for $i ( 0 .. 10 ) {
        $AoA[$i] = [ split " ", <> ];
}

��㋚����˨�������������������������ゃ�������鴻�˨�í�潟�潟����㏍�鴻����с�ŝ�鴻�����菴������������������ŝ�� ��∽�違��篏帥�c�⓾����障���櫝�醇�с��羂������ゃ�������鴻����с����� ��������壩�勰����勤˛��炊����˨�壩����������с��������:

my (@AoA, $i);
for $i ( 0 .. 10 ) {
        $AoA[$i] = [ split " ", scalar(<>) ];
}

��������吾�勉�ŝ����<�㋘�潟�鴻�������☗����� $ref_to_AoA ���篏帥�������������������勉�с�������違�� 篁ヤ����勖����˨�����綽�荀������������с��������:

while (<>) {
        push @$ref_to_AoA, [ split ];
}

��������ф�違�����茵����菴遵�������������������с����障����� ��違�������˨�í�����菴遵����������勉��? �����ŝ�������障����̬����������宴�c�⓾�������勉�ŝ�����紊ф����壠��膣���ŭ撮��ャ����ŝ����障��:

for $x (1 .. 10) {
        for $y (1 .. 10) {
            $AoA[$x][$y] = func($x, $y);
        }
}

for $x ( 3, 7, 9 ) {
        $AoA[$x][20] += func2($x);
}

��������壠嚳莟<����ŝ��荀�膣������≪��絖���������⓾����������í�������˨�壠襲��帥�������障�����: (��ŝ����翫����с��)��������с����ŝ����勉�������˨����勤��膣����篏������冴�����綽�荀����綽������ �����˨�����荀�膣���� undef �����祉����������障�����

�����ŝ����壔�������̬����̬申�����������������������������翫����с����c�⓾����� ��<����c��絋���̬����������������������ŝ�������違�ŝ����ŝ����с��������:

# add new columns to an existing row
push @{ $AoA[0] }, "wilma", "betty";   # ���腓榊�������ŝ����<�㋘�潟�

Perl 5.14 篁ュ����с�壔����������壔�潟�潟����ゃ�˨����堺�ャ�障�������с�����:

push $AoA[0], "wilma", "betty";        # ���藥���������ŝ����<�㋘�潟�

��ŝ����с�����������? ��������壚札������ push() ��勐����違�壠����с�с�壔�ŝ�����絎������勰�������с�����綽�荀���� �����c����������с����� ��������壔����壔��罩c�������壔�������障�������� 絎�������筝�菴違�勉�����藥���������ŝ����<�㋘�潟�鴻�������吾��������茵���� -- �����勐�翫����� -- 罩c��������篏������障��; �����ŝ����<�㋘�潟�鴻��茵���������障�����

��������勐�翫����壔����◑����c�������宴�壔��$AoA[0] �����≪�̹�������ŝ����<�㋘�潟�鴻�� 篆���������⓾�������翫�� ��˨�勉�� ���篏���������������с����� ��ɱ��臂ï�ゃ��絲障����☀�����������������������箴�紊������榊�������障����� ��������壔�����藥���������ŝ����<�㋘�潟�鴻�壔��@{ } ���絽吾�̬�������������ʋ�ɱ��臂ï�ゃ� ��ɱ�������劫�����茵������ŝ����������с��:

my $aref = undef;
push $aref,  qw(some more values);  # WRONG!
push @$aref, qw(a few more);        # ok

�����勖�違��������藥���������ŝ����<�㋘�潟�鴻�勖�壔����������勐�ñ�鴻��篏帥����������ŝ�����罩c���� �����̹�蚊����с�����������: ��������˨�������勉����������ŝ�������潟�若����˨�ŝ����障����� �������𡉴����ŝ�ŝ�若�鴻�с�壔�潟�潟����ゃ�˩賢��˨����í�若����冴��������������茹c����⓾�������������� ��劫����勉�ŝ�ŝ�若�剛札������ Perl 篁ラ����с�勉�水��篏������������������������с�壠��篏������ŝ���������� �����勉��篏帥�������壔����ゃ�с�����篁ヤ����勉�������̥�勛����ゃ��������:

use v5.14;   # needed for implicit deref of array refs by array ops

�����<�ゃ�˨�勐����㏍�˨����勖��腓阪�������吾����鴻����с����� ��������˨��������茯違�������違�������潟�若������𡉴�� perl ��у��茵���������������������������˨�� 罨<�勉�������ŝ����í�若�с�壔�ŝ�

Type of arg 1 to push must be array (not array element) at /tmp/a line 8, near ""betty";"
Execution of /tmp/a aborted due to compilation errors.

罨<�勉�������̬Κ�����ʋ����宴����阪����������障��:

Perl v5.14.0 required--this is only v5.12.3, stopped at /tmp/a line 1.
BEGIN failed--compilation aborted at /tmp/a line 1.

Access and Printing

(��≪�壔�祉�鴻�◑;腓�)

��������í�壔����勉����若�炊����������阪����������ŝ�с����� �����ŝ����壔�í�������������◑�������⓾�障�����? ��������с����㏍��膂≦����̬��膣����筝���ゃ�������阪�������������������������������ŝ����障��:

print $AoA[0][0];

��������勐��絎劫�◒�������阪��������������������罨<�勉�������˨�壩�吾����障��������

print @AoA;             # ���������

��ŝ����ŝ�������������с�壠����˨�ŝ�鴻����吾�勉�ŝ����<�㋘�潟�鴻�������������������с�� perl ��壔����������ɱ�������������у�������������������ŝ�������壔����ŝ����������с����� �����勉�������������ŝ����虁�ɱ����ʃ昆��с�˨�若�������ŝ�������違�ŝ����障�������� ��������壠����眼�勖兄���絖����絲障�������˨�若����с�激�с�˨�鴻�帥�ゃ�˨�� for() ���篏帥�c� 罕�������篏������阪�������障�����

for $aref ( @AoA ) {
        say "\t [ @$aref ],";
}

羞祉��絖����荐���蚊����������勉�ŝ����������勉�������˨�с����障��:

for $i ( 0 .. $#AoA ) {
        say "\t elt $i is [ @{$AoA[$i]} ],";
}

�����������壔����勉�������ŝ�������鴻����������障����� �����眼�勉�˨�若�����羈◐�勉����⓾��������������

for $i ( 0 .. $#AoA ) {
        for $j ( 0 .. $#{$AoA[$i]} ) {
            say "elt $i $j is $AoA[$i][$j]";
        }
}

荀���☗�ゃ����������˨�������壠�����茲������с����� ���������������筝㏍�т�����紊���違��篏帥����亥亜�����˨�с����障��:

for $i ( 0 .. $#AoA ) {
        $aref = $AoA[$i];
        for $j ( 0 .. $#{$aref} ) {
            say "elt $i $j is $AoA[$i][$j]";
        }
}

�����若����障����<����c�◑����˨�������с����㏍�� ��������с�í����с��������:

for $i ( 0 .. $#AoA ) {
        $aref = $AoA[$i];
        $n = @$aref - 1;
        for $j ( 0 .. $n ) {
            say "elt $i $j is $AoA[$i][$j]";
        }
}

�����ŝ����勉����若�炊�������勉�������勉�˨�鴻�帥�� print �����吾����勉�̥�蚊�������ŝ����� 罔�羣���� Dumpvalue ��≪�吾�ャ�若�˨�� Data::Dumper ��≪�吾�ャ�若�˨�� 荀���������������с����������� ��������� Perl �����������㋘��篏帥�c�⓾����⓾��緇������� Perl ��潟�若����������⓾����若�劫櫝�純�ŝ����勉�� ��阪�������障����� 箴�������:

use v5.14;     # using the + prototype, new to v5.14

sub show(+) {
        require Dumpvalue;
        state $prettily = new Dumpvalue::
                            tick        => q("),
                            compactDump => 1,  # comment these two lines out
                            veryCompact => 1,  # if you want a bigger dump
                        ;
        dumpValue $prettily @_;
}

# Assign a list of array references to an array.
my @AoA = (
           [ "fred", "barney" ],
           [ "george", "jane", "elroy" ],
           [ "homer", "marge", "bart" ],
);
push $AoA[0], "wilma", "betty";
show @AoA;

��������壚札筝���勉����勉����阪�������障��:

0  0..3  "fred" "barney" "wilma" "betty"
1  0..2  "george" "jane" "elroy"
2  0..2  "homer" "marge" "bart"

筝���鴻�������������������������������ŝ�������吾�����篋���ゃ�勤�������潟�<�潟����≪�⓾�������������� 篁c�������˩札筝���勉�������ŝ����勉����阪����������障��:

0  ARRAY(0x8031d0)
   0  "fred"
   1  "barney"
   2  "wilma"
   3  "betty"
1  ARRAY(0x803d40)
   0  "george"
   1  "jane"
   2  "elroy"
2  ARRAY(0x803e10)
   0  "homer"
   1  "marge"
   2  "bart"

Slices

(��鴻�í�ゃ��)

紊�罨≦����������勉�鴻�í�ゃ��(茵�������)�����������������勉�с�������違�������������������ʋ兄���絖�篁������� ������綽�荀������������с����������� ��������壠����у�������勉�������勐����с�勛�√�違��篏帥�c�����筝���勤��膣����絲障����������勉� ��������勉�с�����������������絲上����������鴻�í�ゃ�合����勌梢��í�ŝ����勉�壔�ŝ����勉�с�����

篁ヤ����壔����˨�若�����篏帥�c��筝���ゃ�勖��篏������í�勉�������̬����������勌����с����� 紊���� @AoA ��������勉����勉����������с��������篁勐�������⓾����障�����

@part = ();
$x = 4;
for ($y = 7; $y < 13; $y++) {
        push @part, $AoA[$x][$y];
}

�����勉�˨�若�������鴻�í�ゃ�号��膊���̥舟�����������������������с����障��:

@part = @{$AoA[4]}[7..12];

��障����壠�����腥榊�純����ャ��������:

@part = @{ $AoA[4] } [ 7..12 ];

�����ŝ�����荀���〠����������������������障�������������������虁˛��炊����˨�壔�<����c��筝�荀ɱ����с�����

�����若����с�����$x ��� 4..8���$y ��� 7 ������ 12 ������������������ 篋�罨≦����勉�鴻�í�ゃ�� ��� 綽�荀������������������˨��? �����若��������膣���ŝ�������鴻�壔�������с�����������

@newAoA = ();
for ($startx = $x = 4; $x <= 8; $x++) {
        for ($starty = $y = 7; $y <= 12; $y++) {
            $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y];
        }
}

��鴻�í�ゃ�鴻��篏帥�c�⓾�˨�若�����膂≦����˨�с����障��:

for ($x = 4; $x <= 8; $x++) {
        push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ];
}

�����ŝ�������激�ャ�壔�˨��紊������̬����潟�������c�⓾�������勉�ŝ�����map ���篏帥�c�〠❶��勉������� �����������������吾�吟�������������障�����:

@newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;

�����ŝ����勌���夒��筝��櫝В��ŝ�潟�若����˨�������吾�с����祉�㏍�ャ�ŝ�����(�����������壩�ユ����ŭ��絎�)�� 菴醇����������c����⓾�������������⓾�����茯㋚����������勉�奝�c�������с����������� :-) ������腱���������ŝ����勛����眼����c�������������������c�����篏���奝�∽�違����若��莨若�������с�����������

@newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 );
sub splice_2D {
        my $lrr = shift;        # ��ŝ����<�㋘�潟�鴻�勉�ŝ�鴻����勉�ŝ�鴻����吾�勉�ŝ����<�㋘�潟��!
        my ($x_lo, $x_hi,
            $y_lo, $y_hi) = @_;

        return map {
            [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ]
        } $x_lo .. $x_hi;
}

SEE ALSO

perldata, perlref, perldsc

AUTHOR

Tom Christiansen <tchrist@perl.com>

Last update: Tue Apr 26 18:30:55 MDT 2011