keysé¢æ° - ããã·ã¥ã®ãã¹ã¦ã®ãã¼ãåå¾
keysé¢æ°ã使ç¨ããã¨ãハッシュã®ãã¹ã¦ã®ãã¼ãåå¾ãããã¨ãã§ãã¾ããåå¾ãããã¼ã®é åºã¯ä¸å®ã§ãã
# ãã¹ã¦ã®ãã¼ãåå¾ my @keys = keys %hash;
ハッシュのリファレンスãå¼æ°ã¨ãã¦æ¸¡ãå ´åã¯ãããªãã¡ã¬ã³ã¹ããå¿ è¦ãããã¾ãã
# ãã¹ã¦ã®ãã¼ãåå¾ãããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã®å ´åã my @keys = keys %$hash;
ããã·ã¥ã®ãã¼ã¨å¤ããã¹ã¦åºå
keysé¢æ°ã使ã£ã¦ãããã·ã¥ã®ãã¹ã¦ã®ãã¼ã¨ãã¹ã¦ã®å¤ãåºåãã¦ã¿ã¾ãã
# ãã¼ã®è¾æ¸é ã§ä¸¦ã¹æ¿ãã¦åºå my $scores = {Ken => 1, Mike => 2, Rika => 3}; for my $person (sort keys %$scores) { print "$person : $scores->{$person}\n"; }
åºåçµæã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
Ken : 1 Mike : 2 Rika : 3
keysé¢æ°ã§å¾ããããã¼ã¯é åºãä¸å®ã§ããsort関数ã¨çµã¿åãããã¨å®å®ããé åºã§åºåãããã¨ãã§ãã¾ãã
# ãã¼ã®è¾æ¸é ã§ä¸¦ã¹æ¿ãã¦åºå my $scores = {Ken => 1, Mike => 2, Rika => 3}; for my $person (sort keys %$scores) { print "$person : $scores->{$person}\n"; }
ããã«reverse関数ã¨çµã¿åãããã¨ãéé ã§åºåãããã¨ãã§ãã¾ãã
# ãã¼ã®è¾æ¸é ã§ä¸¦ã¹æ¿ãã¦åºå my $scores = {Ken => 1, Mike => 2, Rika => 3}; for my $person (reverse sort keys %$scores) { print "$person : $scores->{$person}\n"; }
ããã·ã¥ã®å¤ããã¹ã¦åå¾ãã
ããã·ã¥ã®å¤ã ãããã¹ã¦åå¾ãããå ´åã¯values関数ã使ç¨ãããã¨ãã§ãã¾ãã
my @values = values %hash;
ãã¼ã¨å¤ã®ãã¢ãé çªã«åå¾ãã
ãã¼ã¨å¤ã®ãã¢ãé çªã«åå¾ãããå ´åã¯eaché¢æ°ã使ããã¨ãã§ãã¾ãã
# (ãã¼, å¤)ã®ãã¢ãé çªã«åå¾ my %age = (Ken => 19, Mike => 34); while (my ($name, $age) = each %age) { ... }
keysé¢æ°ãvaluesé¢æ°ãeaché¢æ°ã¨ããä¼¼ã¦ããé¢æ°ãããã¾ãããããã·ã¥ã®å¦çã«keysé¢æ°ã使ãã®ããå§ãã§ããkeysé¢æ°ã¯ãããã©ã¼ãã³ã¹ããããä¸ã¤ã®ä¸ã§é常ã«æ´»ç¨ã®ããããé¢æ°ã§ãã