Chinaunix

标题: [新手分享]把hash中姓名排序 [打印本页]

作者: laoxia    时间: 2003-01-26 00:29
标题: [新手分享]把hash中姓名排序
本脚本把

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. }
复制代码

作者: 三剑侠客    时间: 2003-07-21 15:34
标题: [新手分享]把hash中姓名排序
请教程序中的\L是啥意思???特殊字符?还是???
请指教!谢谢
作者: gunguymadman007    时间: 2003-12-01 14:43
标题: [新手分享]把hash中姓名排序
就是啊    有没有哪位大虾解释一下
作者: melocy    时间: 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

复制代码

作者: aaronvox    时间: 2006-07-20 13:19
强烈要求大虾解释下....
作者: ulmer    时间: 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 编辑 ]
作者: aaronvox    时间: 2006-07-20 21:23
嘿嘿 不错
以后可以用来吓唬人了
作者: enjoy48    时间: 2006-08-28 18:23
这个不是Learning Perl里面的习题么
作者: alexru    时间: 2006-08-28 18:41
是LEARNING PERL里的习题哈




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2