Program perl 資料結構

gaorongchao1990626發表於2013-05-31

1 陣列的陣列

1.1 建立和訪問二維陣列

建立是最簡單的,舉例:

 1:  @AoA = (["fruit","ball"],
 2:          ["home","bart","marge"],
 3:          ["george","jane","elory"]);
 4:  print $AoA[2]->[1];
 5:  #每一對相鄰的大括號或中括號之間有一個隱含的->,所以也可以這樣寫
 6:  print $AoA[2][1];
 7:  
 8:  #整個列表被封裝在小括號中,而不是中括號中,因為是給列表賦值,
 9:  #而不是給引用賦值,如果要建立一個指向陣列的引用,那麼你要這樣
10:  $ref_to_AoA =[ ["fred","barney","pebbles","bamm"],
11:                 ["homer","bart","marge","maggie",]
12:      ];
13:  print $ref_to_AoA->[2][3];
14:  #注意這裡有在中括號前面的箭頭符號。因為這是引用,所以要在前面加上->
15:  
16:  #還有一點就是可以利用負數索引從陣列後面向前計數,
17:  $AoA[0][-2];
18:  #表示第一行的倒數第二個元素

1.2 自行生長

我們讀入一個資料結構,是一個純文字檔案,每一行是一個結構的一行,並且沒行 包含由空白分割的元素。

 1:  while(<>)
 2:  {
 3:      chomp;
 4:      @tmp = split;
 5:      push @AoA,[@tmp];
 6:  }
 7:  #或者直接不用命名那個陣列
 8:  while(<>)
 9:  {
10:      push @AoA, [split];
11:  }
12:  #如果想要引用指向一個陣列的陣列,可以這樣
13:  while(<>)
14:  {
15:      push @ref_to_AoA,[split];
16:  }
17:  #如果要向陣列中增加列,還是使用簡單的賦值
18:  #如果向一個存在的行中附加一個新列
19:  push @{@AOA[0]},"wilma","betty";
20:  #注意push的引數必須以@開頭

1.3 訪問和列印

 1:  #單元素列印
 2:  print $AoA[3][2];
 3:  #遍歷整個陣列
 4:  for $now (@AoA)
 5:  {
 6:      print "@$row\n";
 7:  }
 8:  #如果你想追蹤腳標
 9:  for $i (0..$#AoA)
10:  {
11:      print "row $i is:@{$AoA[$i]}\n";
12:  }
13:  #或者
14:  for $i (0..$#AoA)
15:  {
16:      for $j (0..$#{$AoA[$i]})
17:      {
18:          print "element $i $j is $AoA[$i][$j]\n";
19:      }
20:  }
21:  #為了簡化上面的程式,我們可以使用臨時變數,讓事情變的簡單。
22:  for $i (0..$#AoA)
23:  {
24:      $row = $AoA[$i];
25:      for $j (0..$@{$row})
26:      {
27:          print "element $i $j is $row->[$j]\n";
28:      }
29:  }

1.4 片段

如果要訪問一個多維陣列的某一片段的元素,你可以用迴圈把變數一個個的取出來。

 1:  @part = ();
 2:  for ($y = 7 ;$y <13 ;$y++)
 3:  {
 4:      push @part,$AoA[4][$y];
 5:  }
 6:  #這個迴圈可以用一個陣列片段來替代
 7:  @part = @{$AoA[4]} [7..12];
 8:  #如果要一個二維片段,同樣也可以通過迴圈來獲得
 9:  #也可以用上面剛學到的方法
10:  for ($x=4;$x<=9;$x++)
11:  {
12:      push @new_AoA,[@{$AoA[$x]} [7..12]];
13:  }
14:  #如果你經常這麼做,可以寫一個子程式,或者到CPAN上下載一個
15:  #PDL(perl data language)模組

2 陣列的雜湊

關於這個名稱,我們可以這樣理解,陣列的雜湊是由“陣列”組成的雜湊, 我們可以首先可以根據雜湊的key快速找到陣列,然後再訪問陣列。

2.1 陣列的雜湊的組成

1:  %HoA = ( flintstones =>["fred","barney"],
2:           jetsons     =>["george","jane","elroy"],
3:      );
4:  #向雜湊增加另一個陣列
5:  $HoA{teletubbies} = ["tinky","dispay","laa",];

2.2 生成陣列的雜湊

從下面格式的檔案中讀取: flintsotnes: fred barney wilma dino
jetsons: george jane elroy
simpsons: homer marge bart
下面是填充陣列的雜湊的技巧

 1:  while(<>)
 2:  {
 3:      next unless s/^(.*?):\s*//;
 4:      $HoA{$i}=[split];
 5:  }
 6:  #或者
 7:  while($line = <>)
 8:  {
 9:      ($who,$rest)=split/:\s*/,$line,2;
10:      @fields=split ' ',$rest;
11:      $HoA{$who}=[@fields];
12:  }
13:  #可以通過一下方法向已存在的陣列追加新的成員
14:  push @{$HoA{flintstones}},"wilma","pebbles";

2.3 訪問和列印陣列

列印所有的元素

 1:  for $family (keys %HoA)
 2:  {
 3:      print "$family: @{$HoA{$family}}\n";
 4:  }
 5:  #你可以為陣列增加索引
 6:  for $family (keys %HoA)
 7:  {
 8:      print "$family: ";
 9:      for $i (0..$#{$HoA{$family}})
10:      {
11:          print " $i = $HoA{$family}[$i]";
12:      }
13:      print "\n";
14:  }
15:  #或者通過以陣列擁有的元素個數對他們排序
16:  for $family (sort {@{$HoA{$b}} <=> @{$HoA{$a}}} keys %HoA)
17:  {
18:      print "$family: @{$HoA{$family}}\n";
19:  }
20:  #也可以以元素的個數對陣列排序,然後以元素的ASCII碼順序進行排序
21:  for $family (sort {@{$HoA{$b}}<=>@{$HoA{$a}}} keys %HoA)
22:  {
23:      print "$family: ",join(",",sort @{$HoA{$family}}),"\n";
24:  }

3 雜湊的陣列

這個名稱和上一個名稱容易混淆,雜湊的陣列,就是由“雜湊“組成的”陣列“, 我們的本意是對一堆資料,想要按照順序訪問他們。並且每一個記錄後面又 包含key/value對

3.1 雜湊的陣列的組成。

 1:  @AoH = ({husband => "barney",
 2:           wife    => "betty",
 3:           son     => "bamm",
 4:          },
 5:          {husband => "george",
 6:           wife    => "jane",
 7:           son     => "elroy",
 8:          },
 9:          {husband => "homer",
10:           wife    => "marge",
11:           son     => "bart",
12:          },
13:  );
14:  
15:  
16:  #向陣列中新增另外一個雜湊,
17:  push @AoH,{husband => "fred",wife => "wilma",daughter=>"pebbles"};

3.2 生成雜湊的陣列

從檔案中讀取下面的格式
husband=fred friend=barney

 1:  while(<>)
 2:  {
 3:      $rec = {};
 4:      for $field (split)
 5:      {
 6:          ($key,$value) = split/=/,$field;
 7:          $rec->{$key} = $value;
 8:      }
 9:      push @AoH,$rec;
10:  }
11:  
12:  #或者用下面更簡單的方法
13:  while(<>)
14:  {
15:      push @AoH,{split /[\s=]+/};
16:  }
17:  
18:  #可以向下面這樣向一個現存的雜湊新增新成員
19:  $AoH[0]{pet} = "dino";
20:  $AoH[2]{pet} = "santa's";

3.3 訪問和列印雜湊的陣列

用下面的方法設定一個特定雜湊的鍵/值對

 1:  $AoH[0]{husband} = "fred";
 2:  #把第二個陣列的丈夫變成大寫
 3:  $AoH[1]{husband} = ~s/(\w)/\u$1/;
 4:  
 5:  #列印所有資料的方法:
 6:  for $href (@AoH)
 7:  {
 8:      print "{ ";
 9:      for $role (keys %$href)
10:      {
11:          print "$role=$href->{$role}";
12:      }
13:      print "}\n";
14:  }
15:  
16:  #帶著索引列印
17:  for $i (0..$#AoH)
18:  {
19:      print "$i is {";
20:      for $role (keys %{$AoH[$i]})
21:      {
22:          print "$role=$AoH[$i]{$role} ";
23:      }
24:      print "}\n";
25:  }

4 雜湊的雜湊

多維雜湊是Perl中最靈活的巢狀結構。

4.1 雜湊的雜湊的組成

 1:  %HoH=(
 2:  flintstones=>
 3:  {
 4:      husband   => "fred",
 5:      pal       => "barney",
 6:  },
 7:  jetsons  =>
 8:  {
 9:      husband   => "george",
10:      wife      => "jane",
11:      "his boy" => "elroy",
12:  },
13:  simpsons =>
14:  {
15:      husband   => "homer",
16:      wife      => "marge",
17:      kid       => "bart",
18:  },
19:      );
20:  #向%HoH中增加一個匿名雜湊
21:  $HoH{mash} = {
22:      captain => "pierce",
23:      major   => "burns",
24:      corporal=> "radar",
25:  }

4.2 生成雜湊的雜湊

從下面格式的檔案中讀取資料:

1:  flintstones: husband=fred pal=barney wife=wilma pet=dino

我們可以使用下面兩個迴圈之一來完成任務

 1:  while(<>)
 2:  {
 3:      next unless s/^(.*?):\S*//;
 4:      $who = $1;
 5:      for $field (split)
 6:      {
 7:          ($key,$value)=split/=/,$field;
 8:          $HoH{$who}{$key} = $value;
 9:      }
10:  }
11:  
12:  #或者
13:  while(<>)
14:  {
15:      next unless s/^(.*?):\S*//;
16:      $who = $1;
17:      $rec = {};
18:      $HoH{$who} = $rec;
19:      for $field (split)
20:      {
21:          ($key,$value)= split/=/,$field;
22:          $rec->{$key}= $value;
23:      }
24:  }

4.3 訪問和列印雜湊的雜湊

 1:  #設定鍵/值對
 2:  $HOH{flintstones}{wife} = "wilma";
 3:  
 4:  #要把某個鍵/值對變成大寫,可對該元素應用替換
 5:  $HoH{jetsons}{'his boy'} =~s/(\w)/\u$1/;
 6:  
 7:  #列印所有的元素,利用先後遍歷內外層雜湊鍵的方法
 8:  for $family (keys %HoH)
 9:  {
10:      print "$family: ";
11:      for $role (keys %{$HoH{$family}})
12:      {
13:          print "$role = $person ";
14:      }
15:      print "\n";
16:  }
17:  
18:  #在非常大的雜湊中,可以用each同時把鍵和值都檢索出來
19:  while (($family,$roles) = each %HoH)
20:  {
21:      print "$family: ";
22:      while (($role,$person) = each %$roles)
23:      {
24:          print "$role = $person";
25:      }
26:      print "\n";
27:  }
28:  
29:  #排序輸出
30:  for $family (sort keys %HoH)
31:  {
32:      print "$family: ";
33:      for $role (sort keys %{$HoH{$family}})
34:      {
35:          print "$role=$HoH{$family}{$role} ";
36:      }
37:      print "\n";
38:  }

5 函式的雜湊

6 以上內容摘自大駱駝,謝謝

Author: GRC <grc@grc>

Date: 2013-05-31 19:22:49 CST

HTML generated by org-mode 6.33x in emacs 23

相關文章