- 多位数组
- my %HAO;
- for $i(0..9){
- for $j(0..9) {
- $HAO[$i][$j] = $i.$j;
- }
- }
- for $x(0..$#HAO) {
- for $y(0..$#{$HAO[$x]}) {
- print $HAO[$x][$y]." ";
- }
- print "\n";
- }
- PS D:\> perl 01.pl
- 00 01 02 03 04 05 06 07 08 09
- 10 11 12 13 14 15 16 17 18 19
- 20 21 22 23 24 25 26 27 28 29
- 30 31 32 33 34 35 36 37 38 39
- 40 41 42 43 44 45 46 47 48 49
- 50 51 52 53 54 55 56 57 58 59
- 60 61 62 63 64 65 66 67 68 69
- 70 71 72 73 74 75 76 77 78 79
- 80 81 82 83 84 85 86 87 88 89
- 90 91 92 93 94 95 96 97 98 99
- #结果
- 哈希,数组
- my %HAO = (
- fine => ["df","ee"],
- nine => ["fine","ok"],
- wcc => ["you","fuck"]
- );
- foreach $key(keys %HAO) {
- for $j(0..$#{$HAO{$key}}) {
- print $HAO{$key}[$j]." ";
- }
- print "\n";
- }
- #显示结果
- PS D:\> perl 01.pl
- df ee
- you fuck
- fine ok
- PS D:\>
- ##################################################################
- my %month = (
- "01" => "Jan",
- "02" => "Feb",
- "03" => "Mar",
- "04" => "Apr",
- "05" => "May",
- "06" => "Jun",
- "07" => "Aug"
- );
- $point = \%month;
- print "\n address of hash = $point\n";
- foreach $i(sort keys %month) {
- print "$i $$point{$i}\n"; #
- # print "$i $month{$i}\n";
- }
- print "\n\n";
- foreach $i(sort keys %$point) {
- print "$i $$point{$i}\n"; #
- }
- 输出
- address of hash = HASH(0x285bac)
- 01 Jan
- 02 Feb
- 03 Mar
- 04 Apr
- 05 May
- 06 Jun
- 07 Aug
- 01 Jan
- 02 Feb
- 03 Mar
- 04 Apr
- 05 May
- 06 Jun
- 07 Aug
- 多维数组
- 1 #!/usr/bin/perl
- 2 #
- 3 # Using Multi-dimensional Array references
- 4 #
- 5 $line = ['solid', 'black', ['1','2','3'] , ['4', '5', '6']];
- 6 print "\$line->[0] = $line->[0] \n";
- 7 print "\$line->[1] = $line->[1] \n";
- 8 print "\$line->[2][0] = $line->[2][0] \n";
- 9 print "\$line->[2][1] = $line->[2][1] \n";
- 10 print "\$line->[2][2] = $line->[2][2] \n";
- 11 print "\$line->[3][0] = $line->[3][0] \n";
- 12 print "\$line->[3][1] = $line->[3][1] \n";
- 13 print "\$line->[3][2] = $line->[3][2] \n";
- 14 print "\n"; # The obligatory output beautifier.
- 结果输出如下:
- $line->[0] = solid
- $line->[1] = black
- $line->[2][0] = 1
- $line->[2][1] = 2
- $line->[2][2] = 3
- $line->[3][0] = 4
- $line->[3][1] = 5
- $line->[3][2] = 6
- 那么三维数组又如何呢?下面是上例略为改动的版本。
- 1 #!/usr/bin/perl
- 2 #
- 3 # Using Multi-dimensional Array references again
- 4 #
- 5 $line = ['solid', 'black', ['1','2','3', ['4', '5', '6']]];
- 6 print "\$line->[0] = $line->[0] \n";
- 7 print "\$line->[1] = $line->[1] \n";
- 8 print "\$line->[2][0] = $line->[2][0] \n";
- 9 print "\$line->[2][1] = $line->[2][1] \n";
- 10 print "\$line->[2][2] = $line->[2][2] \n";
- 11 print "\$line->[2][3][0] = $line->[2][3][0] \n";
- 12 print "\$line->[2][3][1] = $line->[2][3][1] \n";
- 13 print "\$line->[2][3][2] = $line->[2][3][2] \n";
- 14 print "\n";
- 结果输出如下:
- $line->[0] = solid
- $line->[1] = black
- $line->[2][0] = 1
- $line->[2][1] = 2
- $line->[2][2] = 3
- $line->[2][3][0] = 4
- $line->[2][3][1] = 5
- $line->[2][3][2] = 6
- 访问第三层元素的方式形如$line->[2][3][0],类似于C语言中的
转载于:https://blog.51cto.com/624569/444998