雜湊在Perl中是以百分比符號(%)作為表示,變數的命名方式則維持一貫原則,也就是可以包含字母,數字及底線的字串,但是不能以數字作為開頭。所以你可以像這樣的方式定義一個雜湊變數 :
接著底下要來介紹一些常用的 Hash 操作與對應範例代碼.
Initialize a hash :
Assigning an empty list is the fastest way.
範例代碼 :
Clear (or empty) a hash :
If you literally want to empty the contents of a hash.
範例代碼 :
- for (keys %hash)
- {
- delete $hash{$_};
- }
範例代碼 :
- for (keys %$href)
- {
- delete $href->{$_};
- }
In the solutions below, quotes around the keys can be omitted when the keys are identifiers.
範例代碼 :
- $hash{ 'key' } = 'value'; # hash
- $hash{ $key } = $value; # hash, using variables
- $href->{ 'key' } = 'value'; # hash ref
- $href->{ $key } = $value; # hash ref, using variables
The following statements are equivalent, though the second one is more readable.
範例代碼 :
- %hash = ( 'key1', 'value1', 'key2', 'value2', 'key3', 'value3' ); # First one
- %hash = (
- key1 => 'value1',
- key2 => 'value2',
- key3 => 'value3',
- ); # Second one
範例代碼 :
- my %hash_copy = %hash; # copy a hash
- my $href_copy = $href; # copy a hash ref
The solution differs for a hash and a hash reference, but both cases can use the delete function.
範例代碼 :
- delete $hash{$key};
- delete $hash_ref->{$key};
The actions below print the key/value pairs.
範例代碼 :
- my %form = ();
- %form = (
- key1 => 'value1',
- key2 => 'value2',
- key3 => 'value3'
- );
- print "Using while loop: \n";
- while(my($key, $value) = each(%form)){
- print "\t$key => $value\n";
- }
- print "Using for loop: \n";
- for my $key (keys %form) {
- my $value = $form{$key};
- print "\t$key => $value\n";
- }
範例代碼 :
- my %form = ();
- %form = (
- key1 => 'value1',
- key2 => 'value2',
- key3 => 'value3'
- );
- print "size of hash form (implicit): " . keys(%form) . "\n"; # method1 : implicit scalar context
- my $i = 0;
- $i += scalar keys %form; # method2 : explicit scalar context
- print "size of hash form (explicit): " . $i;
範例代碼 :
- sub foo
- {
- my $hash_ref;
- $hash_ref->{'key1'} = 'value1';
- $hash_ref->{'key2'} = 'value2';
- $hash_ref->{'key3'} = 'value3';
- return $hash_ref;
- }
- my $hash_ref = foo();
- for my $key (keys %$hash_ref)
- {
- print "Key=$key ; Value=$hash_ref->{$key}...\n"
- }
The following two solutions are equivalent, except for the way the look. In my opinion the second approach is clearer.
範例代碼(1) :
- $patch="test";
- $os = "Win";
- $arch = "86";
- $info = "XP";
- $requiredPatches_href->{$patch}->{os} = $os;
- $requiredPatches_href->{$patch}->{arch} = $arch;
- $requiredPatches_href->{$patch}->{info} = $info;
- print "requiredPatches_href->$patch->os= " . $requiredPatches_href->{$patch}->{os} . "...\n";
- print "requiredPatches_href->$patch->arch= " . $requiredPatches_href->{$patch}->{arch} . "...\n";
- print "requiredPatches_href->$patch->info= " . $requiredPatches_href->{$patch}->{info} . "...\n";
- $requiredPatches_href->{ $patch } = {
- os => $os,
- arch => $arch,
- info => $info,
- };
底下範例代碼使用函式 foo 讀取 /etc/passwd 後, 使用 "用戶名" 當 key ; 並將該用戶資訊用 hash 的 hash 存放後返回 hash :
範例代碼 :
- sub foo
- {
- my ( $login, $p, $uid, $gid, $gecos, $dir, $s );
- my %HoH = ();
- my $file = '/etc/passwd';
- open( PASSWD, "< $file" ) or die "Can't open $file : $!";
- while(
) { - ( $login, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' );
- $HoH{ $login }{ 'uid' } = $uid;
- $HoH{ $login }{ 'gid' } = $gid;
- $HoH{ $login }{ 'dir' } = $dir;
- }
- close PASSWD;
- return \%HoH;
- }
範例代碼(1) :
- my $rHoH = foo();
- my( $uid, $gid, $dir );
- for my $login ( keys %$rHoH ) {
- $uid = $rHoH->{ $login }->{ 'uid' }; # method 1 most readable
- $gid = ${ $rHoH->{ $login } }{ 'gid' }; # method 2
- $dir = ${ ${ $rHoH }{ $login } }{ 'dir' }; # method 3 least readable
- print "uid: $uid, gid: $gid, dir, $dir.\n";
- }
- my $rHoH = foo();
- for my $k1 ( sort keys %$rHoH ) {
- print "k1: $k1\n";
- for my $k2 ( keys %{$rHoH->{ $k1 }} ) {
- print "k2: $k2 $rHoH->{ $k1 }{ $k2 }\n";
- }
- }
範例代碼 :
- while( my ($k, $v) = each %$hash_ref ) {
- print "key: $k, value: $v.\n";
- }
範例代碼 :
- print "Value EXISTS, but may be undefined.\n" if exists $hash{ $key };
- print "Value is DEFINED, but may be false.\n" if defined $hash{ $key };
- print "Value is TRUE at hash key $key.\n" if $hash{ $key };
補充說明 :
* Perl 學習手扎 - 5. 雜湊(Hash)
* [Perl] Something about hash of Perl
* PERL: How to remove an element from hash or array?
* Perl Hash Howto
* Perl Tutorial - hashes
沒有留言:
張貼留言