deleteé¢æ° - ããã·ã¥ã®ãã¼ã®åé¤
deleteé¢æ°ã使ç¨ããã¨ãハッシュã®ãã¼ãåé¤ãããã¨ãã§ãã¾ãããã¼ã«å¯¾å¿ããå¤ãåé¤ããã¾ããæ»ãå¤ã¯åé¤ããããã¼ã«å¯¾å¿ããå¤ã§ãã
my $value = delete $hash{$key};
ハッシュのリファレンスã«å¯¾ãã¦deleteé¢æ°ã使ç¨ããå ´åã¯æ¬¡ã®ããã«æ¸ãã¾ãã
my $value = delete $hash->{$key};
deleteé¢æ°ã使ã£ã¦ãageã¨ãããã¼ãåé¤ãããµã³ãã«ã§ãã
# ageã¨ãããã¼ãåé¤ my %person = (name => 'Ken', age => 19); delete $person{age};
ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã®å ´åã®ãµã³ãã«ã§ãã
# ãã¼ãåé¤ããªãã¡ã¬ã³ã¹ã®å ´åã my $person = {name => 'Ken', age => 19}; delete $person->{age};
ãµã³ãã«ããã°ã©ã
ããã·ã¥ã®ãã¼ãåé¤ãããµã³ãã«ã§ãåºåãè¦ãã¾ãã
use strict; use warnings; use Data::Dumper; # çå¾ã®æ°å¦ã®ç¹æ° my %math_scores = ( Aiko => 89, Kenta => 0, Taro => undef ); # 1: æåã®ç¶æ print "1: æåã®ç¶æ \n"; if (exists $math_scores{Aiko}) { print "\$math_scores{Aiko} ã¯åå¨ãã¾ãã\n"; } print Data::Dumper->Dump([\%math_scores] , ['*math_scores']); print "\n\n"; # deleteã§ãã¼ãåé¤ delete $math_scores{Aiko}; # 2: deleteã§åé¤ããç¶æ print "2: deleteã§åé¤ããç¶æ \n"; if (exists $math_scores{Aiko}) { print "\$math_scores{Aiko} ã¯åå¨ãã¾ãã\n"; } else{ print "\$math_scores{Aiko} ã¯åå¨ãã¾ããã\n"; } print Dumper \%math_scores; print "\n";
å®è¡çµæ
1: æåã®ç¶æ $math_scores{Aiko} ã¯åå¨ãã¾ãã %math_scores = ( 'Aiko' => 89, 'Kenta' => 0, 'Taro' => undef ); 2: deleteã§åé¤ããç¶æ $math_scores{Aiko} ã¯åå¨ãã¾ããã %math_scores = ( 'Kenta' => 0, 'Taro' => undef );