[メモ]Perlのハッシュの例
Perlではハッシュを使う時が、多々あるのですが、使い方が??になる時があります。
いくつかのハッシュを引数とした時の例です。
1 2 3 4 5 6 7 8 9 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 |
#!/usr/bin/perl # use strict; use warnings; use Data::Dumper; sub fncTest1{ my( %yy ) = @_; print Dumper %yy; my ( $test1, $test2 ) = @yy{'Test1', 'Test2'}; print ("Test1:$test1 / Test2:$test2\n"); foreach my $key( sort keys %yy ) { print "$key,$yy{$key}\n"; } } fncTest1( Test1 => '1', Test2 => '2' ); my $test01 = 'Test1'; my $test02 = 'Test2'; fncTest1( $test01 => '11', $test02 => '12' ); my %dd = ( $test01 => '21', $test02 => '22' ); fncTest1( %dd ); my %yy; $yy{'Test1'} = '31'; $yy{'Test2'} = '32'; fncTest1( %yy ); my %xx; $xx{$test01} = '41'; $xx{$test02} = '42'; fncTest1( %xx ); |
$ ./test.pl
$VAR1 = ‘Test2’;
$VAR2 = ‘2’;
$VAR3 = ‘Test1’;
$VAR4 = ‘1’;
Test1:1 / Test2:2
Test1,1
Test2,2
$VAR1 = ‘Test2′;
$VAR2 = ’12’;
$VAR3 = ‘Test1′;
$VAR4 = ’11’;
Test1:11 / Test2:12
Test1,11
Test2,12
$VAR1 = ‘Test2′;
$VAR2 = ’22’;
$VAR3 = ‘Test1′;
$VAR4 = ’21’;
Test1:21 / Test2:22
Test1,21
Test2,22
$VAR1 = ‘Test2′;
$VAR2 = ’32’;
$VAR3 = ‘Test1′;
$VAR4 = ’31’;
Test1:31 / Test2:32
Test1,31
Test2,32
$VAR1 = ‘Test2′;
$VAR2 = ’42’;
$VAR3 = ‘Test1′;
$VAR4 = ’41’;
Test1:41 / Test2:42
Test1,41
Test2,42