Perl小知识点之排序sort
脚本这种东西,就是要常用,否则一段时间不用就生疏了,因此决定时时记一些小知识点,一来回顾一下,二来需要的时候可以迅速获得提示。
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的更多相关文章
- Java学习过程中的总结的小知识点(长期更新)
Java学习过程中的总结的小知识点 (主要是自己不会的知识和容易搞错的东西) 计算某个程序运行的时间 long stime=System.currentTimeMillis(); copy3(file ...
- 转:详细解说 STL 排序(Sort)
详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...
- C#、Java中的一些小知识点总结(持续更新......)
前言:在项目中,有时候一些小的知识,总是容易让人忽略,但是这些功能加在项目中往往十分的有用,因此笔者在这里总结项目中遇到的一些实用的小知识点,以备用,并持续更新...... 1.禁用DataGridV ...
- 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...
链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...
- 详细解说 STL 排序(Sort)(转)
作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...
- ACM_小凯的排序(字符串)
小凯的排序 Time Limit: 2000/1000ms (Java/Others) Problem Description: 调皮的小凯喜欢排序,拿到什么东西都要排一下序.现在他觉得单一的递增递减 ...
- sort排序,按指定字段进去重,sort -t "^" -k 8 -su,ls给文件名中数字排序sort -k1.5n,Tab符要转义
sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 ...
- 刚接触Linux,菜鸟必备的小知识点(一)
身为一个将要大四的学生,而且还是学计算机的没有接触过linux简直是羞愧难当.这个假期做了一个软件测试员,必须要熟悉linux的操作,所以对于我这个菜鸟我也就说几点比较重要的小知识点吧. 第一.cd指 ...
- 【转】HTML5的小知识点小集合
html5的小知识点小集合 html5知识 1. Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...
随机推荐
- Thrift框架介绍
1.前言 Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目.Thrift通过一个中间语言(IDL, 接口定义语言)来定义RPC的接口和 ...
- Mapreduce执行过程分析(基于Hadoop2.4)——(三)
4.4 Reduce类 4.4.1 Reduce介绍 整完了Map,接下来就是Reduce了.YarnChild.main()—>ReduceTask.run().ReduceTask.run方 ...
- HDU-3874 Necklace 线段树+离线
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 比较简单的题,题意也好懂. 先O(n)求每个数左边第一次出现的与他相同的数的位置l[i].对询问 ...
- C++Bulder DataSnap 内存泄露元凶
DSServerClass1 DSServerClass1DestroyInstance void __fastcall TServerContainer1::DSServerClass1Destro ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- python 应用xml.dom.minidom读xml
xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...
- Codeforces 27E. Number With The Given Amount Of Divisors (暴力)
题目链接:http://codeforces.com/problemset/problem/27/E 暴力 //#pragma comment(linker, "/STACK:1024000 ...
- C:函数
函数 函数:都是实现一定的功能.具有特定功能的代码段.凡是由系统提供的函数就是库函数,自己写的函数就是自定义函数. 如何定义一个函数 : 函数类型修饰符 函数名 (函数参数) { 函数语句 ...
- Git版本管理:Windows下Git配置与使用指南
简要介绍:Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 一.安装 软件:msysGit-fullinstall-1.8.1.2 打开之后设置安装路径,默认为 ...
- 为什么要把js代码写到<!--//-->中
是为了兼容,不支持js的浏览器会把其中的内容当做html注释.