Perl 字符串操作 以及 自定义排序
admin
2023-07-28 08:40:06
0
  1. #!/usr/bin/perl -w  

  2. #########################################################################  

  3. # File Name: test6.pl  

  4. #########################################################################  

  5.   

  6. #index(str,substr,position),在大字符串中查找  

  7. #position可以设置起始位置,返回位置数  

  8. my $stuff = "hello word";  

  9. my $where = index($stuff, "wor");  

  10. print "where: $where\n";  

  11.   

  12. my $where1 = index($stuff, "w");  

  13. print "where1: $where1\n";  

  14.   

  15. my $where2 = index($stuff, "w", 6);  

  16. print "where2: $where2\n";  

  17.   

  18. # substr($string, $init_position, $length) 处理部分字符串  

  19. $substr1 = substr("hello world", 6, 5); #得到substr1: world  

  20. print "substr1: $substr1\n";  

  21. #可以作替换字符串使用  

  22. substr($substr1,0,0)="hello ";  

  23. print "substr1: $substr1\n"#substr1: hello world  

  24. #替换功能也可以使用第四个参数  

  25. $string = "hello world";  

  26. substr($string, 0, 5, "xxxxx");  

  27. print "string: $string\n";  

  28.   

  29. #sprintf 格式化数据   

  30. $year = "2014";  

  31. $month = 8;  

  32. $day = 2.0;  

  33. $data_tag = sprintf("%s/%d/%.3f", $year, $month, $day);  

  34. print "data_tag: $data_tag\n"#data_tag: 2014/8/2.000  

  35.   

  36. #排序  

  37. #排序子函数,按数字排序  

  38. sub by_num{  

  39.     $a <=> $b  

  40. }  

  41. @nums = (1,4,22,5,33,10,12);  

  42. print "nums: @nums\n";  

  43. @result1 = sort @nums;  

  44. print "result1: @result1\n"#只会把数字当作字符排序  

  45. @result2 = sort by_num @nums;  

  46. print "result2: @result2\n"#会根据数值大小进行排序  

  47.   

  48. #对hash排序  

  49. my %socre = (  

  50.     "kevin" => 100,  

  51.     "xiang" => 50,  

  52.     "jie" => 150,  

  53.     "xxx" => 1,  

  54.     "yyy" => 50  

  55. );  

  56. @socre1 = %socre;  

  57. print "socre1: @socre1\n"#socre1: xiang 50 jie 150 kevin 100 xxx 1 yyy 50  

  58.   

  59. #排序子函数,根据value 从小到大   

  60. #如果是数字直接用 <=> 如果是字符串用 cmp  

  61. sub by_socre_value{  

  62.     $socre{$a} <=> $socre{$b}  

  63. }  

  64. @socre2 = sort by_socre_value keys %socre;  

  65. print "socre2: @socre2\n"#socre2: xxx xiang yyy kevin jie  

  66.   

  67.   

  68. %socre = (  

  69.     "kevin" => 100,  

  70.     "xiang" => 50,  

  71.     "jie" => 150,  

  72.     "xxx" => 1,  

  73.     "yyy" => 50  

  74. );  

  75. #根据value 从小到大, 如果value相同,则根据key字母反序  

  76. #如果还有很多个条件,可以加or再限制  

  77. sub by_socre_value_and_key{  

  78.     $socre{$a} <=> $socre{$b}  

  79.     or  

  80.     $b cmp $a;  

  81. }  

  82. @socre3 = sort by_socre_value_and_key keys %socre;  

  83. print "socre3: @socre3\n"#socre3: xxx yyy xiang kevin jie  




相关内容

热门资讯

浙江宣传:“走个面儿”咋就没面... “咱北京两千多万人口,您受累,您走个面儿,把这第一波的票房带起来,咱就有了。”某知名导演的新片首映礼...
辞职声明仅95秒遭质疑,韩国队... 【环球时报综合报道】美加墨世界杯小组赛出局后,韩国队主教练洪明甫当地时间28日在墨西哥的韩国队大本营...
美媒爆料:美军第五舰队总部遭伊... 据美国《华尔街日报》27日报道,其通过对卫星图像、社交媒体视频和五角大楼记录的分析发现,今年2月底至...
英国智库给菲律宾GDP增速“浇... 【环球时报特约记者 叶满】英国经济研究机构凯投宏观发布的最新一期《亚洲经济展望》报告(以下简称“报告...
欧洲持续高温,有华人用冰箱降温... 连日来,欧洲多国迎来罕见极端高温天气,法国、德国、意大利等地气温持续飙升,部分地区突破40摄氏度。受...
伊副外长强调船只须按“伊朗线路... 伊朗外交部副部长加里巴巴迪当地时间29日晚间在接受采访时强调,所有船只均须按照“伊朗线路”通过霍尔木...
委内瑞拉强震已致1719人死亡 当地时间29日,委内瑞拉全国代表大会主席罗德里格斯通报,地震已造成该国1719人死亡,5034人受伤...
铋晟新材料申请氯氧化铋基复合材... 国家知识产权局信息显示,江苏铋晟新材料有限公司申请一项名为“一种氯氧化铋基复合材料及其制备方法和用途...
韩国政府将投资千万亿韩元于AI... 韩国总统李在明29日在总统府青瓦台主持召开会议,公布总额超千万亿韩元的半导体、物理人工智能(AI)和...
以色列防长称以伊可能随时再起冲... △卡茨(资料图)据以色列方面29日消息,以国防部长卡茨当天表示,鉴于复杂的安全局势和在黎巴嫩的军事行...