ããã·ã¥ã®ãªãã¡ã¬ã³ã¹
Perl
›
ãªãã¡ã¬ã³ã¹
›
here
Perlã«ã¯ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¨ãããã¼ã¿æ§é ãé »ç¹ã«ç»å ´ãã¾ãããã®ãã¼ã¸ã§ã¯ãリファレンスã®ä¸ããããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã詳ããåãä¸ãã¾ãã
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¨ã¯
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¨ã¯ãハッシュã«å¯¾ãããªãã¡ã¬ã³ã¹ã®ãã¨ã§ãããªãã¡ã¬ã³ã¹ã¨ããã®ã¯ãæãã¨ãããã¨ã表ãã¾ããããã·ã¥ã«å¯¾ãã¦ããªãã¡ã¬ã³ã¹çæåã\ãã使ç¨ããã¨ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãä½æãããã¨ãã§ãã¾ãã
# ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãä½æ my %score = (math => 68, english => 90); my $score_ref = \%score;
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ãスカラ変数ã«ä»£å ¥ãããã¨ããããã¨ã«æ³¨æãã¦ãã ããã
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã®ã¤ã¡ã¼ã¸ã以ä¸ã«æ¸ãã¦ã¿ã¾ãã
# ã¤ã¡ã¼ã¸ %score $score_ref -------> (math => 68, english => 90)
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ãããã·ã¥ãã®ãã®ã§ã¯ãªããããã·ã¥ãæã示ããã®ãã表ç¾ãã¾ãã
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ä»£å ¥ã§ä¸èº«ãã³ãã¼ãããªã
ããã·ã¥ã¨ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã®éããããããè¦ã¦ããã¾ãããã
ããã·ã¥ã¯ãä»£å ¥ã«ãã£ã¦ãä¸èº«ããã¹ã¦ã³ãã¼ããã¾ãããããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ã³ãã¼ãè¡ããã¾ããã
# ããã·ã¥ã¯å¤ãã³ãã¼ããã my %score1 = (math => 68, english => 90); my %score2 = %score1; # ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯å¤ã¯ã³ãã¼ãããªã my %score1 = (math => 68, english => 90); my $score_ref1 = \%score1; my $score_ref2 = $score_ref1;
以ä¸ã¯ã¤ã¡ã¼ã¸ã§ããããã·ã¥ã®å ´åã¯ãä¸èº«ãã¹ã¦ãã³ãã¼ãããã®ã«å¯¾ãã¦ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ãä¸èº«ã§ã¯ãªãããªãã¡ã¬ã³ã¹ãã®ãã®ãã³ãã¼ããã¾ãã
# ã¤ã¡ã¼ã¸ %score1 %score2 (math => 68, english => 90) (math => 68, english => 90) $score_ref1 ------------> $score_ref2 ------------> (math => 68, english => 90)
サブルーチンã«æ¸¡ãå ´åã¯ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã渡ãæ¹ããä¸èº«ã®ã³ãã¼ãè¡ãããªãã®ã§ãå¹çãè¯ããªãã¾ãã
# ãµãã«ã¼ãã³ã«ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã渡ã my_func($score_ref1);
ç¡åããã·ã¥çæå
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ããªãã¡ã¬ã³ã¹çæåã\ãã使ç¨ããã¨ä½æã§ãã¾ãããå°ãæéããããã¾ããããã«ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãä½æããããã«ã無名ハッシュ生成子ã¨å¼ã°ããæ¼ç®åãå®ç¾©ããã¦ãã¾ãã
my $score_ref = {math => 68, english => 90};
é常ã®ããã·ã¥ãã()ãã§ä½æãããã®ã«å¯¾ãã¦ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¯ç¡åããã·ã¥çæåã{}ãã使ã£ã¦çæããã¦ããã®ã«æ³¨æãã¦ãã ããã
Perlã®ã½ã¼ã¹ã³ã¼ãã§ã¯ããã®è¨æ³ãé »ç¹ã«ç»å ´ãã¾ããããã·ã¥ãªã®ããããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãªã®ããã½ã¼ã¹ã³ã¼ãããèªã¿åããã¨ã大åã§ãã
# ããã·ã¥ my %score =(math => 68, english => 90); # ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ my $score = {math => 68, english => 90};
ããªãã¡ã¬ã³ã¹
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãçæããã«ã¯ããªãã¡ã¬ã³ã¹çæåã使ãã¾ãããä»åº¦ã¯å対ã«ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãããããã·ã¥ãåãåºãã¦ã¿ã¾ãããã
ãã®ããã«ã¯ããªãã¡ã¬ã³ã¹ã¨ããæä½ãè¡ãã¾ãã
my $score_ref = {math => 68, english => 90}; # ããªãã¡ã¬ã³ã¹ my %score = %{$score_ref};
ããªãã¡ã¬ã³ã¹ã¯ã%{ããã·ã¥ã®ãªãã¡ã¬ã³ã¹}ãã¨ããè¨æ³ã使ã£ã¦è¡ããã¨ãã§ãã¾ãã
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãã¹ã«ã©å¤æ°ã®å ´åã¯ã{}ããçç¥ãããã¨ãã§ãã¾ãã
my %score = %$score_ref;
æ¨æºé¢æ°ã«å¼æ°ã¨ãã¦æ¸¡ã
keys関数ãvalues関数ãeach関数ãªã©ã®ããã·ã¥ã«é¢ããé¢æ°ã¯ãå¼æ°ã«ããã·ã¥ãã¨ãã¾ãããã®ãããããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã渡ãããå ´åã¯ãããªãã¡ã¬ã³ã¹ããå¿ è¦ãããã®ã§æ³¨æãã¦ãã ããã
# ããªãã¡ã¬ã³ã¹ãã¦æ¨æºé¢æ°ã«æ¸¡ã keys %$hash; values %$hash; each %$hash;
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ããããã·ã¥ã®å¤ãåãåºã
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ããããã·ã¥ã®å¤ãåãåºãã«ã¯ã¢ãã¼æ¼ç®åã->ãã使ããã¨ãã§ãã¾ãã
# ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ããããã·ã¥ã®å¤ã®åå¾ my $score_math = $score_ref->{math}; # ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ããããã·ã¥ã®å¤ãè¨å® $score_ref->{math} = 67;
ã¢ãã¼æ¼ç®åã使ããã¦ããå ´åã¯ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã§ããããã§ãªãå ´åã¯ãããã·ã¥ã§ãããããã½ã¼ã¹ã³ã¼ãã®ä¸ã§ããã£ããèªã¿åããããã«ãªã£ã¦ãã ããã
# ããã·ã¥ã®å¤ã®åå¾ my $score_math = $score{math}; # ããã·ã¥ã®å¤ãè¨å® $score{math} = 67;
ã¢ãã¼æ¼ç®åã®ãããªãã ãã§ãããã·ã¥ãªã®ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ãªã®ããç°ãªãã®ã§ããã£ããè¦ããã¨ã大åã§ãã
é åã®ãªãã¡ã¬ã³ã¹
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã¨åããããé »ç¹ã«å©ç¨ããããã®ã¨ãã¦é åã®ãªãã¡ã¬ã³ã¹ãããã¾ãã
my $nums_ref = [1, 3, 5];
é åã®ãªãã¡ã¬ã³ã¹ã«ã¤ãã¦ã¯ä»¥ä¸ã§è©³ãã解説ãã¦ãã¾ãã
Perl
›
ãªãã¡ã¬ã³ã¹
›
here