#!/usr/bin/perl 1 # 2 # Using Associative Array references 3 # 4 %month = ( 5 '01', 'Jan', 6 '02', 'Feb', 7 '03', 'Mar', 8 '04', 'Apr', 9 '05', 'May', 10 '06', 'Jun', 11 '07', 'Jul', 12 '08', 'Aug', 13 '09', 'Sep', 14 '10', 'Oct', 15 '11', 'Nov', 16 '12', 'Dec', 17 ); 18 19 $pointer = %month; 20 21 printf "n Address of hash = $pointern "; 22 23 # 24 # The following lines would be used to print out the 25 # contents of the associative array if %month was used. 26 # 27 # foreach $i (sort keys %month) { 28 # printf "n $i $$pointer{$i} "; 29 # } 30 31 # 32 # The reference to the associative array via $pointer 33 # 34 foreach $i (sort keys %$pointer) { 35 printf "$i is $$pointer{$i} n"; 36 }
结果输出如下:
$ mth Address of hash = HASH(0x806c52c) 01 is Jan 02 is Feb 03 is Mar 04 is Apr 05 is May 06 is Jun 07 is Jul 08 is Aug 09 is Sep 10 is Oct 11 is Nov 12 is Dec
1 #!/usr/bin/perl 2 # 3 # Using Array references 4 # 5 %weekday = ( 6 '01' => 'Mon', 7 '02' => 'Tue', 8 '03' => 'Wed', 9 '04' => 'Thu', 10 '05' => 'Fri', 11 '06' => 'Sat', 12 '07' => 'Sun', 13 ); 14 $pointer = %weekday; 15 $i = '05'; 16 printf "n ================== start test ================= n"; 17 # 18 # These next two lines should show an output 19 # 20 printf '$$pointer{$i} is '; 21 printf "$$pointer{$i} n"; 22 printf '${$pointer}{$i} is '; 23 printf "${$pointer}{$i} n"; 24 printf '$pointer->{$i} is '; 25 26 printf "$pointer->{$i}n"; 27 # 28 # These next two lines should not show anything 29 # 30 printf '${$pointer{$i}} is '; 31 printf "${$pointer{$i}} n"; 32 printf '${$pointer->{$i}} is '; 33 printf "${$pointer->{$i}}"; 34 printf "n ================== end of test ================= n"; 35
结果输出如下:
================== start test ================= $$pointer{$i} is Fri ${$pointer}{$i} is Fri $pointer->{$i} is Fri ${$pointer{$i}} is ${$pointer->{$i}} is ================== end of test =================