免费注册 查看新帖 |

Chinaunix

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

[新手分享]把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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2003-07-21 15:34 |只看该作者

[新手分享]把hash中姓名排序

请教程序中的\L是啥意思???特殊字符?还是???
请指教!谢谢

论坛徽章:
0
3 [报告]
发表于 2003-12-01 14:43 |只看该作者

[新手分享]把hash中姓名排序

就是啊    有没有哪位大虾解释一下

论坛徽章:
0
4 [报告]
发表于 2006-07-19 22:01 |只看该作者
感觉应该是\L***\E组合,转换为小写。


  1. [mhung@mybsd ~]$ cat  w1.pl
  2. #!/usr/bin/perl

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

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

  12. foreach (@keys) {
  13.   print "$last_name{$_}, $_
  14. ";
  15. }

  16. [mhung@mybsd ~]$ ./w1.pl
  17. FLINSTONE, PEBBLES
  18. Flintstone, Wilma
  19. Rubble, Bamm-Bamm
  20. Rubble, Barney
  21. flintstone, fred
  22. rubbel, betty

复制代码

论坛徽章:
0
5 [报告]
发表于 2006-07-20 13:19 |只看该作者
强烈要求大虾解释下....

论坛徽章:
0
6 [报告]
发表于 2006-07-20 20:09 |只看该作者
原帖由 aaronvox 于 2006-7-20 13:19 发表
强烈要求大虾解释下....


simplyfine just set string as lowercase to compare with each other.
you can use lc() instead of \L...\E.

see perldoc -f lc
lc EXPR -- Returns a lowercased version of EXPR.  
                This is the internal function implementing the "\L" escape in double-quoted strings.
                ......
see perldoc perlre
  \L          lowercase till \E (think vi)
  \U          uppercase till \E (think vi)
  \E          end case modification (think vi)


i.e.:

  1. # bad writewise:
  2. #my %last_name = qw {
  3. #  fred flintstone Wilma Flintstone Barney Rubble
  4. #  betty rubbel Bamm-Bamm Rubble PEBBLES FLINSTONE
  5. #};
  6. # better
  7. my %last_name =(
  8.   'fred'      => 'flintstone',
  9.   'Wilma'     => 'Flintstone',
  10.   'Barney'    => 'Rubble',
  11.   'betty'     => 'rubbel',
  12.   'Bamm-Bamm' => 'Rubble',
  13.   'PEBBLES'   => 'FLINSTONE',
  14. );
  15. print Dumper(\%last_name);
  16. my @sorted_by_keys_no_matter_lower_or_upercase =  sort {
  17.         #"\L$last_name{$a}\E" cmp "\L$last_name{$b}\E"  or "\L$a\E" cmp "\L$b\E"
  18.         lc($a) cmp lc($b)     # compare with key
  19.         or
  20.         lc($last_name{$a}) cmp lc($last_name{$b})    # compare with value
  21.     }  keys %last_name;    # array

  22. print "Sorted keys:\n";
  23. print Dumper(\@sorted_by_keys_no_matter_lower_or_upercase);

复制代码



Best,
      ulmer


-----------
Just 4 Fun

[ 本帖最后由 ulmer 于 2006-7-20 20:12 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-07-20 21:23 |只看该作者
嘿嘿 不错
以后可以用来吓唬人了

论坛徽章:
0
8 [报告]
发表于 2006-08-28 18:23 |只看该作者
这个不是Learning Perl里面的习题么

论坛徽章:
0
9 [报告]
发表于 2006-08-28 18:41 |只看该作者
是LEARNING PERL里的习题哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP