脚本这种东西,就是要常用,否则一段时间不用就生疏了,因此决定时时记一些小知识点,一来回顾一下,二来需要的时候可以迅速获得提示。

Sort by number

You could now write a numeric sort subroutine like this:

 sub by_number {
# a sort subroutine, expect $a and $b
if ($a < $b) { – } elsif ($a > $b) { } else { }
}

To use the sort subroutine, just put its name (without an ampersand) between the keyword sort and the list you’re sorting. This example puts a numerically sorted list of numbers into @result:

my @result = sort by_number @some_numbers;

Notice that you don’t have to do anything in the sort subroutine to declare $a and $b and set their values—and if you did, the subroutine wouldn’t work right. We just let Perl set up $a and $b for us, so all you need to write is the comparison.

In fact, you can make it even simpler (and more efficient).  Perl has a convenient shortcut to use to write it. In this case, you use the spaceship operator (<=>). This operator compares two numbers and returns –1, 0, or 1 as needed to sort them numerically.

sub by_number { $a <=> $b }

Sort by strings

three-way string-comparison operator: cmp. These two are easy to remember and keep straight.

  • >=有两种可能的返回值,而<=>有三种可能的返回值,因为>=是两个字符,<=>是三个字符。同样道理,
  • ge有两种可能的返回值,而cmp有三种可能的返回值,因为ge有两个字符,而cmp有三个字符。
sub by_code_point { $a cmp $b }
my @strings = sort by_code_point @any_strings;

But you can use cmp to build a more complex sort order, like a case-insensitive sort:

sub case_insensitive { "\L$a" cmp "\L$b" }

In this case, you’re comparing the string from $a (forced to lowercase) against the string from $b (forced to lowercase), giving a case-insensitive sort order. 

Hash sort: Sorting a Hash by Value

my %score = ("barney" => , "fred" => , "dino" => );
my @winners = sort by_score keys %score;
sub by_score { $score{$b} <=> $score{$a} }

注意这里$b在$a前面,因为我们希望是按照分数降序排列。

如果有分数相同的怎么办?按照名字字母排序:

my %score = (
"barney" => , "fred" => ,
"dino" => , "bamm-bamm" => ,
); my @winners = sort by_score_and_name keys %score;
sub by_score_and_name {
$score{$b} <=> $score{$a} # by descending numeric score
or
$a cmp $b # code point order by name
}

当分数相同是,$score{$b} <=> $score{$a}返回0,因此执行短路操作符or之后的语句:$a cmp $b,按照名字字母排序。

<over>

以上摘自:《Learning Perl 6th Edition》

Perl小知识点之排序sort的更多相关文章

  1. Java学习过程中的总结的小知识点(长期更新)

    Java学习过程中的总结的小知识点 (主要是自己不会的知识和容易搞错的东西) 计算某个程序运行的时间 long stime=System.currentTimeMillis(); copy3(file ...

  2. 转:详细解说 STL 排序(Sort)

    详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...

  3. C#、Java中的一些小知识点总结(持续更新......)

    前言:在项目中,有时候一些小的知识,总是容易让人忽略,但是这些功能加在项目中往往十分的有用,因此笔者在这里总结项目中遇到的一些实用的小知识点,以备用,并持续更新...... 1.禁用DataGridV ...

  4. 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...

    链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...

  5. 详细解说 STL 排序(Sort)(转)

    作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...

  6. ACM_小凯的排序(字符串)

    小凯的排序 Time Limit: 2000/1000ms (Java/Others) Problem Description: 调皮的小凯喜欢排序,拿到什么东西都要排一下序.现在他觉得单一的递增递减 ...

  7. sort排序,按指定字段进去重,sort -t "^" -k 8 -su,ls给文件名中数字排序sort -k1.5n,Tab符要转义

    sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 ...

  8. 刚接触Linux,菜鸟必备的小知识点(一)

    身为一个将要大四的学生,而且还是学计算机的没有接触过linux简直是羞愧难当.这个假期做了一个软件测试员,必须要熟悉linux的操作,所以对于我这个菜鸟我也就说几点比较重要的小知识点吧. 第一.cd指 ...

  9. 【转】HTML5的小知识点小集合

    html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...

随机推荐

  1. Mysql报错:1172 - Result consisted of more than one row

    执行mysql函数时报错:1172 - Result consisted of more than one row 函数语句中select into语句中WHERE account = userNam ...

  2. WS之cxf与spring整合2

    在action中加入webservice

  3. openstack network

  4. html5 +css3 第一章学习和笔记

    各位同学.身为本版的斑竹.,希望各位童鞋都能学到Html5 .特此没两天更新一个学习笔记和大家一起学习Html5.... 语法改变       1.新增的元素和废除的元素       2.新增的属性和 ...

  5. 8.1搜索专练DFS和BFS

    这是第一次全部做出来的依次练习了,有一些都是做过两遍了的,但是还是错了几回,更多时候我还是应该多注意下细节,就好像章爷笑我 的一样,像什么vis[]标记没清0,什么格式错误,还有什么题目没看清,还是的 ...

  6. How Tomcat Works(九)

    本文接下来描述servlet容器是怎样管理其相关组件的生命周期的,首先本人描述一下事件监听模式,也可以称为观察者模式,该模式分为以下角色 即抽象主题角色 具体主题角色 抽象观察者角色及具体观察者角色, ...

  7. halcon的性能

    ·满足您各类机器视觉应用需求的完善的开发库 ·包含匹配,识别,定位及1D,2D,3D测量等多种高级算法 ·强大,易用的工具加速您的开发进程 ·与Linux/UNI及Windows(包括×64)兼容,避 ...

  8. ucenter同步登陆机制

    有个ucenter可以用来实现多个站点同时登陆同时退出,用户同步的功能. 首先站点都要引入一个uc_client这样一个客户端,以登陆为例,登陆时首先会调用一个外部函数,uc_client/clien ...

  9. spring事物配置注意事项

    <tx:advice id="txAdvice" transaction-manager="transactionManager">  <tx ...

  10. C#使用SMTP发送邮件

    需要用到的命名空间: using System.Net.Mail; using System.IO; using System.Text.RegularExpressions; using Syste ...