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 ...
随机推荐
- Eclipse使用技巧总结
Eclipse设置工作空间的字符编码: 打开eclipse开发界面,依次点击Window->Preferences->General->Workspace 修改Text file e ...
- group_concat 长度限制,排序和设置分隔符
select aid,group_concat(bid order by bid separator ',') as bid_str from tbl group by aid; SET GLOBAL ...
- AS3垃圾回收整理
AS3垃圾回收整理 转自 http://bbs.wefdc.com/thread-2366-1-1.html 来源页面: http://www.adobe.com/devnet/actionscrip ...
- 安装完 MySQL 后必须调整的 10 项配置(转)
英文原文:10 MySQL settings to tune after installation 译文原文:安装完 MySQL 后必须调整的 10 项配置 当我们被人雇来监测MySQL性能时,人们希 ...
- 导出Excel Gridview
/// <summary> /// 定义导出Excel的函数 /// </summary> /// <param name="FileType ...
- Dependency Injection学习笔记
component把需要依赖者(CoffeeMaker)和供应提供者(Heater, Pump)联系起来 使用 区别:上的的依赖是内部创建的,下面的依赖是外面传进来的 注入方式
- putty
PUTTY详解 http://www.wifi-robots.com/thread-915-1-1.html 如何使用Putty远程(SSH)管理Linux VPS http://www.vpser. ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...
- HDU1000
哈哈A+B #include<stdio.h> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF ...
- c++中__declspec用法总结
“__declspec”是Microsoft c++中专用的关键字,它配合着一些属性可以对标准C++进行扩充.这些属性有:align.allocate.deprecated. dllexport.dl ...