Perlã®ããã·ã¥ãç解ããã
Perl
›
ããã·ã¥
Perlã®ãããã·ã¥ãã«ã¤ãã¦ã®è§£èª¬ã§ããPerlã®ããã·ã¥ã¯ãä»ã®è¨èªã§ããã¨ããã®ãé£æ³é åãã®ãã¨ã§ãã
é åã§ã¯ãçªå·ãæå®ãã¦è¦ç´ ãåãåºããã¨ãã§ãã¾ãããããé£æ³é åãã§ã¯ãæååããã¼ã¨ãã¦æå®ãã¦ãè¦ç´ ãåãåºããã¨ãã§ãã¾ãã
ããã·ã¥ã®åºç¤
ããã·ã¥ã®åºç¤ãããã·ã¥ã®å®£è¨ã¨åæåãããã·ã¥ã®åºåã¨ä»£å ¥ãããã·ã¥ã¯ãæååããæ·»ãåã¨ãã¦ãå¤ãåå¾ã§ããé åã ã¨èããã°ãããæååããã¼ã¨ãã¦ãå¤ãåãåºããã¨ãã§ããã
ããã·ã¥ã®å®£è¨ã¨åæå
ããã·ã¥ã®å®£è¨ã¨åæåã¯æ¬¡ã®ããã«ãã¦è¡ãã¾ãã
my %fathers = ('Taro' => 'Kenji', 'Naoko' => 'Yoshio'); #ããã㯠my %fathers = (Taro => 'Kenji', Naoko => 'Yoshio');
ãå¤æ°ã®æ¥é è¾ã¯ãã¼ã»ã³ãè¨å·ã§ãã%ãã(ãã¼1 => å¤1, ãã¼2 => å¤2, ...)ãTaroã®ç¶è¦ªã¯Kenji, Naokoã®ç¶è¦ªã¯Yoshioã¨èªããã¨ãã§ãã
ããã·ã¥ã®è¦ç´ ã®åºå
print $mothers{'Taro'};
ãåºåããå ´åã¯ã%ã§ã¯ãªãã¦$ ãåä¸ã®è¦ç´ ãæ±ãå ´åã¯$ ã{ }ã使ã£ã¦ãã¼ãæå®ããã( é åã®å ´åã¯[ ]ã§ãã )ã{} ã¯ãã©ã±ããã¨èªã¿ã¾ãã( [] ã¯ãã¬ã¼ã¹ã () ã¯ãã£ã )
ããã·ã¥ã®è¦ç´ ã®ä»£å ¥
$mothers{'Naoko'} = 'Haruna';
ããã·ã¥ã®åè¦ç´ ã®å¦ç
ããã·ã¥ã®æä½
- キーの存在を調べる - exists関数ãéè¦ã
- ハッシュのキーを削除する - delete関数ãéè¦ã
- ハッシュのキーと値を入れ替える reverse関数
- ハッシュスライス ・・・ ハッシュの複数の値を取り出す
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ä»¥ä¸ã®ããã«ä½ããã¨ãã§ãã¾ãã
my $math_scores_ref = \%math_scores;
%ã®åã« \ ãã¤ããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãçæãçæãããªãã¡ã¬ã³ã¹ããã¹ã«ã©ã¼å¤æ° $math_score_href ã«ä»£å ¥
ç¡åããã·ã¥ã使ã£ã¦ãç´æ¥ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãä½ãã
my $math_scores_ref2 = { Aiko => 89, Kenta => 100, Taro => 34, };
ã( ) ã®ä»£ããã« { } ã使ã£ã¦ãç´æ¥ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãä½æã§ãã
ããã·ã¥ã®è¦ç´ ã«ã¢ã¯ã»ã¹ãã
$math_scores_ref->{Aiko}
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ããããã·ã¥ãåãåºã
ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ããããã·ã¥ãåãåºãã«ã¯ããªãã¡ã¬ã³ã¹ãè¡ãã¾ãã
my %hash = %$hash_ref; my %hash = %{$hash_ref};
ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãããªãã¡ã¬ã³ã¹ãã¦ããã·ã¥ãåãåºããã¨ãã§ãã¾ãã
my %math_scores = %$math_scores;
ã%$math_scoresã§ããªãã¡ã¬ã³ã¹ãã¦ããã·ã¥ãåãåºããã¨ãã§ããã
ã²ã¨ã¤ã®è¦ç´ ããå¿ è¦ã¨ããªãã¨ãã¯ã->{key}ãã使ããã¨ãã§ãã¾ãã
$math_scores->{Taro};
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã«ã¤ãã¦ã¯ãã¡ãã§è©³ãã解説ãã¦ãã¾ãã
ããã·ã¥ã使ã£ãå¤æ¬¡å ãã¼ã¿æ§é
ããã·ã¥ã使ã£ãå¤æ¬¡å ãã¼ã¿æ§é ã«ã¤ãã¦ã¯ã以ä¸ã®è¨äºã§è©³ãã解説ãã¦ãã¾ãããé åã®ããã·ã¥ããããã·ã¥ã®é åããããã·ã¥ã®ããã·ã¥ãã«ã¤ãã¦å¦ã¶ãã¨ãã§ãã¾ãã
ã³ã©ã ãé£æ³é åãããã·ã¥ã¨å¼ã¶ããã«ãªã£ãã®ã¯Perlã®ããã
Perlã¯ãé£æ³é åãè¨èªã®æ©è½ã¨ãã¦åãå ¥ããã¨ããç´ æ´ããã決å®ããããã¨ã¦ã¤ããªã便å©ã ãããã¯ã大æåã§ãä»ã®ã»ã¼ãã¹ã¦ã®ã¹ã¯ãªããè¨èªã¯ãå½±é¿ãåãã¦ãè¨èªèªä½ã«é£æ³é åãåãå ¥ãã¦ããã
é£æ³é åã¨ããååã¯é·ãã£ãã®ããPerlã¯ãããããã·ã¥ã¨å¼ã¶ãã¨ã«ãããããã¯ãããã·ã¥é¢æ°ããã®é£æ³ã ãã
ã§ããæå¤ã¨ã´ã£ããã¨ããã¼ã¿æ§é ã表ç¾ãã¦ããããã«ãæãããã
ãµã³ãã«ã³ã¼ã
ããã·ã¥ã®åºç¤
ãããã·ã¥ã®åºç¤ãããã·ã¥ã®å®£è¨ã¨åæåãããã·ã¥ã®åºåã¨ä»£å ¥ã®ãµã³ãã«ã§ãã
use strict; use warnings; # ããã·ã¥ã¨ã¯ # æååããæ·»ãåã¨ãã¦ãå¤ãåå¾ã§ããé åã ã¨èããã°ããã # æååããã¼ã¨ãã¦ãå¤ãåãåºããã¨ãã§ããã # ããã·ã¥ã®å®£è¨ã¨åæå( å¤æ°ã®æ¥é è¾ã¯ã% ) # ( ãã¼1 => å¤1, ãã¼2 => å¤2, ... ) # Taroã®ç¶è¦ªã¯Kenji, Naokoã®ç¶è¦ªã¯Yoshioã¨èªã my %fathers = ('Taro' => 'Kenji', 'Naoko' => 'Yoshio'); # ããPerlçãªæ¸ãæ¹ # => ã®å·¦å´ã®''ã¯çç¥ã§ãã my %mathers = (Taro => 'Tomoko', Naoko => 'Shizuka'); # 1: ããã·ã¥ã®è¦ç´ åºå print "1:ããã·ã¥ã®è¦ç´ ã®åºå\n"; # åºåããå ´åã¯ã%ã§ã¯ãªãã¦$ # {} ãã©ã±ããã使ã print "\$mathers{'Taro'} = ", $mathers{'Taro'}, "\n"; print "\n"; # 2: ããã·ã¥ã®è¦ç´ ã®ä»£å ¥ print "2:ããã·ã¥ã®è¦ç´ ã®ä»£å ¥\n"; $mathers{'Naoko'} = 'Haruna'; print "\$mathers{'Naoko'} = ", $mathers{'Naoko'} . "\n";
ãå®è¡çµæ
1:ããã·ã¥ã®è¦ç´ ã®åºå $mothers{'Taro'} = Tomoko 2:ããã·ã¥ã®è¦ç´ ã®ä»£å ¥ $mothers{'Naoko'} = Haruna
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã使ã£ãã®ãµã³ãã«ã§ãã
use strict; use warnings; use Data::Dumper; # çå¾ã®æ°å¦ã®ç¹æ° my %math_scores = ( Aiko => 89, Kenta => 100, Taro => 34, ); # 1:ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ %ã®åã« \ ãã¤ããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãçæã # çæãããªãã¡ã¬ã³ã¹ããã¹ã«ã©ã¼å¤æ° $math_scores_ref ã«ä»£å ¥ my $math_scores_ref = \%math_scores; print "1: ããã·ã¥ã®ãªãã¡ã¬ã³ã¹\n"; print Data::Dumper->Dump([$math_scores_ref], ['$math_scores_ref']); print "\n\n"; # 2:ç¡åããã·ã¥( { } )ã使ã£ã¦ãç´æ¥ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãä½ãã my $math_scores_ref2 = { Aiko => 89, Kenta => 100, Taro => 34, }; print "2: ç¡åããã·ã¥\n"; print Data::Dumper->Dump([$math_scores_ref2], ['$math_scores_ref2']); print "\n\n"; # 3:ããã·ã¥ã®è¦ç´ ã«ã¢ã¯ã»ã¹ããã($hash_ref->{key}) print "3: ããã·ã¥ã®è¦ç´ ã«ã¢ã¯ã»ã¹ããã\n"; print "\$math_scores_ref->{Aiko} = $math_scores_ref->{Aiko}\n"; print "\$math_scores_ref->{Kenta} = $math_scores_ref->{Kenta}\n";
ãå®è¡çµæ
1: ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ $math_scores_ref = { 'Aiko' => 89, 'Kenta' => 100, 'Taro' => 34 }; 2: ç¡åããã·ã¥ $math_scores_ref2 = { 'Aiko' => 89, 'Kenta' => 100, 'Taro' => 34 }; 3: ããã·ã¥ã®è¦ç´ ã«ã¢ã¯ã»ã¹ããã $math_scores_ref->{Aiko} = 89 $math_scores_ref->{Kenta} = 100
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã®ããªãã¡ã¬ã³ã¹
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãããªãã¡ã¬ã³ã¹ãããµã³ãã«ã§ãã
use strict; use warnings; use Data::Dumper; # çå¾ã®æ°å¦ã®ç¹æ° my $math_scores = { Taro => 89, Naoko => 54, Kenji => 54 }; # 1:ããªãã¡ã¬ã³ã¹ãã¦ããã·ã¥ã®ãã¹ã¦ã®è¦ç´ ãåºåãã print "1: ããªãã¡ã¬ã³ã¹ãã¦ããã·ã¥ã®ãã¹ã¦ã®è¦ç´ ãåºåãã\n"; for my $key (keys %$math_scores){ print "\$math_scores->{$key} = " . $math_scores->{$key} . "\n"; } print "\n"; # 2:ã²ã¨ã¤ã®è¦ç´ ãåç §ããã¨ãã¯ã-> æ¼ç®åã使ãã print "2: ã²ã¨ã¤ã®ãã¼ãåé¤\n"; delete $math_scores->{Taro}; print Data::Dumper->Dump([$math_scores], ['$math_scores']);
ãå®è¡çµæ
1: ããªãã¡ã¬ã³ã¹ãã¦ããã·ã¥ã®ãã¹ã¦ã®è¦ç´ ãåºåãã $math_scores->{Kenji} = 54 $math_scores->{Taro} = 89 $math_scores->{Naoko} = 54 2: ã²ã¨ã¤ã®ãã¼ãåé¤ $math_scores = { 'Kenji' => 54, 'Naoko' => 54 };