#!/usr/bin/perl use strict; use warnings; my %cities = ( "한국" => "서울", "일본" => "도쿄", "그리스" => "아테네", "미국" => "워싱턴", "프랑스" => "파리", ); print "\n<key를 가나다순으로 소팅>\n"; foreach $_ (sort keys %cities) { print "$_ : $cities{$_}\n"; } print "\n\n<value를 가나다순으로 소팅>\n"; foreach $_ (sort { $cities{$a} cmp $cities{$b} } keys %cities) { print "$_ : $cities{$_}\n"; }
#!/usr/bin/perl use strict; use warnings; my %num = ( 4343 => 4, 88 => 1215, 0.1 => -12, 1212.1 => 124.0, 542.0 => 54, 42.0 => 0, 87 => 99, ); print "\n\n<key를 숫자 크기순으로 소팅>\n"; foreach $_ (sort {$a <=> $b} keys %num) { print "$_ : $num{$_}\n"; } print "\n\n<value를 숫자 크기순으로 소팅>\n"; foreach $_ (sort { $num{$a} <=> $num{$b} } keys %num) { print "$_ : $num{$_}\n"; }