免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3873 | 回复: 0
打印 上一主题 下一主题

[新手分享]把hash中姓名排序 [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-01-26 00:29 |显示全部楼层 |倒序浏览
本脚本把

my %last_name = qw {
  fred flintstone Wilma Flintstone Barney Rubble
  betty rubbel Bamm-Bamm Rubble PEBBLES FLINSTONE
};

进行sorting. 具体要求是:

Make a program that will print the following hash''s data sorted in case-insensitive
alphabetical order by last name ( 首先按姓排序, 不计大小写)
When the last names are the same. sort those by first name( again, without
regard for case)  (当姓一样时,按名排序,不计大小写)
All of the people with same family name should be grouped together( 所有
姓相同的项应相邻)
The names should be printed with the same capitaliztion as shown here (
最后结果不改变原来的字母大小写)

# ./perlt
FLINSTONE, PEBBLES
flintstone, fred
Flintstone, Wilma
rubbel, betty
Rubble, Bamm-Bamm
Rubble, Barney



  1. #!/usr/local/bin/perl
  2. my %last_name = qw {
  3.   fred flintstone Wilma Flintstone Barney Rubble
  4.   betty rubbel Bamm-Bamm Rubble PEBBLES FLINSTONE
  5. };

  6. my @keys = sort {
  7. "L$last_name{$a}" cmp "L$last_name{$b}"
  8.   or
  9. "L$a" cmp "L$b"
  10. } keys %last_name;

  11. foreach (@keys) {
  12.   print "$last_name{$_}, $_
  13. ";
  14. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP