關於perl中sort的用法舉例
@articles = sort @files;FfvB/
©LeoHacks -- LeoHacks,Leohacks的聚散地 &c-;'
# same thing, but with explicit sort routineh:
@articles = sort {$a cmp $b} @files;C
©LeoHacks -- LeoHacks,Leohacks的聚散地 EngxL
# now case-insensitivelyfN)
@articles = sort {uc($a) cmp uc($b)} @files;uqy%
©LeoHacks -- LeoHacks,Leohacks的聚散地 +w
# same thing in reversed orderaUWnWJ
@articles = sort {$b cmp $a} @files;]LD$
©LeoHacks -- LeoHacks,Leohacks的聚散地 B"
# sort numerically ascendingw:E&CI
@articles = sort {$a <=> $b} @files;;_to=v
©LeoHacks -- LeoHacks,Leohacks的聚散地 X8i-
# sort numerically descendingB
@articles = sort {$b <=> $a} @files;MaFs[y
©LeoHacks -- LeoHacks,Leohacks的聚散地 <
# this sorts the %age hash by value instead of key@>f
# using an in-line functionV'
@eldest = sort { $age{$b} <=> $age{$a} } keys %age;>k<
©LeoHacks -- LeoHacks,Leohacks的聚散地 {EM5&J
# sort using explicit subroutine name^h2
sub byage {`
$age{$a} <=> $age{$b};# presuming numeric<(D T=
}"
@sortedclass = sort byage @class;D(:Q=
©LeoHacks -- LeoHacks,Leohacks的聚散地
sub backwards { $b cmp $a }cb,r4
@harry = qw(dog cat x Cain Abel);%9&
@george = qw(gone chased yz Punished Axed);q-u-|l
print sort @harry;O>GSu
# prints AbelCaincatdogxk9
print sort backwards @harry;"j=?-(
# prints xdogcatCainAbel{_
print sort @george, 'to', @harry;vLL`
# prints AbelAxedCainPunishedcatchaseddoggonetoxyzPe OjW
©LeoHacks -- LeoHacks,Leohacks的聚散地 xe(?
# inefficiently sort by descending numeric compare usingyw;So
# the first integer after the first = sign, or the7SFa
# whole record case-insensitively otherwise'<y^[
©LeoHacks -- LeoHacks,Leohacks的聚散地 s
@new = sort {[email protected]
($b =~ /=(/d+)/)[0] <=> ($a =~ /=(/d+)/)[0]T
||&bJ&3
uc($a) cmp uc($b)0eP
} @old;Owgh
©LeoHacks -- LeoHacks,Leohacks的聚散地 _+W
# same thing, but much more efficiently;RbPL
# we'll build auxiliary indices insteadvI{I'
# for speedG-"z
@nums = @caps = ();'+
for (@old) {&_z{G|
push @nums, /=(/d+)/;-u8Df
push @caps, uc($_);!o
}
©LeoHacks -- LeoHacks,Leohacks的聚散地 HnY
@new = @old[ sort {[@LbDY
$nums[$b] <=> $nums[$a]{
||©LeoHacks -- LeoHacks,Leohacks的聚散地 oiS
$caps[$a] cmp $caps[$b]4
} 0..$#old8Q`V
];Q~a
©LeoHacks -- LeoHacks,Leohacks的聚散地 Zmh$9X
# same thing, but without any tempsL'
@new = map { $_->[0] }uM%q
sort { $b->[1] <=> $a->[1]k"o#
||+E3Rz
$a->[2] cmp $b->[2]/W~
} map { [$_, /=(/d+)/, uc($_)] } @old;Nueq1*
©LeoHacks -- LeoHacks,Leohacks的聚散地 pEG
# using a prototype allows you to use any comparison subroutinemJJ9
# as a sort subroutine (including other package's subroutines)5<Z
package other;([
sub backwards ($$) { $_[1] cmp $_[0]; }# $a and $b are not set herey.H=S
©LeoHacks -- LeoHacks,Leohacks的聚散地 MFx0u
package main;j
@new = sort other::backwards @old; :c0_}s
©LeoHacks -- LeoHacks,Leohacks的聚散地 cv !
©LeoHacks -- LeoHacks,Leohacks的聚散地 S-a
map 用法Z
©LeoHacks -- LeoHacks,Leohacks的聚散地 bS/
map BLOCK LIST -%FNL
map EXPR,LIST ]tJo
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation. In scalar context, returns the total number of elements so generated. Evaluates BLOCK or EXPR in list context, so each element of LIST may produce zero, one, or more elements in the returned value.6za/q
©LeoHacks -- LeoHacks,Leohacks的聚散地 {J:sd
@chars = map(chr, @nums); =gq4'
©LeoHacks -- LeoHacks,Leohacks的聚散地 .8)"-h
©LeoHacks -- LeoHacks,Leohacks的聚散地 ]
©LeoHacks -- LeoHacks,Leohacks的聚散地 0+5<;
©LeoHacks -- LeoHacks,Leohacks的聚散地 <8WI$m
translates a list of numbers to the corresponding characters. AndRP:B+
©LeoHacks -- LeoHacks,Leohacks的聚散地 Qn6|k`
%hash = map { getkey($_) => $_ } @array; 1fHyD4
©LeoHacks -- LeoHacks,Leohacks的聚散地 "{aG
©LeoHacks -- LeoHacks,Leohacks的聚散地 iL
©LeoHacks -- LeoHacks,Leohacks的聚散地 Q
©LeoHacks -- LeoHacks,Leohacks的聚散地 CgkQ
is just a funny way to writeTY
©LeoHacks -- LeoHacks,Leohacks的聚散地 %
%hash = ();RS
foreach $_ (@array) {]L-z
$hash{getkey($_)} = $_;JQ=lwP
} ,
©LeoHacks -- LeoHacks,Leohacks的聚散地 bs/
©LeoHacks -- LeoHacks,Leohacks的聚散地 g9|{Tr
©LeoHacks -- LeoHacks,Leohacks的聚散地 }^if
©LeoHacks -- LeoHacks,Leohacks的聚散地 {cm|-
Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST. While this is useful and supported, it can cause bizarre results if the elements of LIST are not variables. Using a regular foreach loop for this purpose would be clearer in most cases. See also /grep for an array composed of those items of the original list for which the BLOCK or EXPR evaluates to true.Y
©LeoHacks -- LeoHacks,Leohacks的聚散地 rM
{ starts both hash references and blocks, so map { ... could be either the start of map BLOCK LIST or map EXPR, LIST. Because perl doesn't look ahead for the closing } it has to take a guess at which its dealing with based what it finds just after the {. Usually it gets it right, but if it doesn't it won't realize something is wrong until it gets to the } and encounters the missing (or unexpected) comma. The syntax error will be reported close to the } but you'll need to change something near the { such as using a unary + to give perl some help:-}6
©LeoHacks -- LeoHacks,Leohacks的聚散地 S<=g#g
%hash = map { "/L$_", 1 } @array # perl guesses EXPR. wrongs
%hash = map { +"/L$_", 1 } @array # perl guesses BLOCK. rightNmRvW#
%hash = map { ("/L$_", 1) } @array # this also works$:kPa
%hash = map { lc($_), 1 } @array # as does this.bmRV
%hash = map +( lc($_), 1 ), @array # this is EXPR and works!"5Mn3
©LeoHacks -- LeoHacks,Leohacks的聚散地 ?
%hash = map ( lc($_), 1 ), @array # evaluates to (1, @array) cHt&F
©LeoHacks -- LeoHacks,Leohacks的聚散地 #gP1lY
©LeoHacks -- LeoHacks,Leohacks的聚散地 v^
©LeoHacks -- LeoHacks,Leohacks的聚散地 :1^hn
©LeoHacks -- LeoHacks,Leohacks的聚散地 FH
or to force an anon hash constructor use +{/@kA]k
©LeoHacks -- LeoHacks,Leohacks的聚散地 /[email protected]
@hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end ZS
©LeoHacks -- LeoHacks,Leohacks的聚散地 tJ/0S
©LeoHacks -- LeoHacks,Leohacks的聚散地 (H=Em:
©LeoHacks -- LeoHacks,Leohacks的聚散地 j
©LeoHacks -- LeoHacks,Leohacks的聚散地 ZHe01G
and you get list of anonymous hashes each with only 1 entry.|
相關推薦
關於perl中sort的用法舉例
# sort lexically-. @articles = sort @files;FfvB/©LeoHacks -- LeoHacks,Leohacks的聚散地 &c-;' # same thing, but with explicit sort routineh: @article
C++STL中sort用法介紹
自定義STL中sort的排序規則 前情提要: 0、要使用sort,首先需要包含標頭檔案< algorithm> 1、sort函式可以指定兩個引數,也可以指定三個引數。 (1)第一個是要排序
STL中sort用法小例項
#include<iostream> #include<vector> #include<algorithm> using namespace std; #define stlforeach(type,iter,container) ty
startActivityForResult和setResult在實際專案中的用法舉例
我們知道startActivityForResult( )較startActivity( )而言,不僅可以跳轉到下一個activity,而且當被跳轉的頁面呼叫finish()後,可以自動跳回
C++中sort用法
網址:http://www.cnblogs.com/sooner/archive/2012/04/18/2455279.html STL裡面有個sort函式,可以直接對陣列排序,複雜度為n*log2(n)。sort()定義在在標頭檔案<algorithm>中
perl中grep,sort,map用法總結
簡簡單單講map(一)map函式map BLOCK LISTmap EXPR, LISTmap函式對LIST裡的每個元素按BLOCK或EXPR進行計算,遍歷LIST時,臨時將LIST裡的每個元素賦值給$_變數。map對每次的計算返回一個結果列表,它在列表上下文裡計算BLOCK或EXPR。每個LIST元素可能在
django models 中choices之用法舉例
bsp har student 兩個 包含 rfi 一個 nbsp save CHOICES常用做單選屬性,下面舉例在django models中人物性別的用法: 我們先定義一個模型,名字為Students ,這個Students 包含了名字和性別兩個字段,代碼如下: f
C++中sort函式用法
C++中sort函式用法 排序示例: 輸入兩個數n,t,其中n是待排的結構體個數,t=0代表用降序排序,t = 1表示用升序排序 例如這樣: 例示: jack 70 peter 96 Tom 70 smith 67 從高到低 成績 peter 96 jack 70
python中list的count和index用法舉例
>>> str = [1,2,3,4,5] #定義一個列表 >>> str *= 3 #列表*3 >>> str [1, 2,
c++中sort()函式的用法簡介
程式碼: #include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main() {
Vue.js中ref ($refs)用法舉例總結
原文地址:http://www.cnblogs.com/xueweijie/p/6907676.html <div id="app"> <input type="text" ref="input1"/> <button @click
c++中sort基礎用法
用法一:陣列排序 對一個數組進行升序排序 #include <algorithm> #include <iostream> #include <cstdio> using namespace std; int m
STL中list容器中sort函式用法
首先,宣告一下,由於list基礎結構是連結串列,不能直接用下標【】來直接取某一元素。 1、當list 中型別是int或者string型別時,直接呼叫sort函式,即 void ListSortTest1() {list<string> num;num.push_
STL中常用的vector,map,set,sort 用法
1. push_back() 在陣列的最後新增一個數據 2. pop_back() 去掉陣列的最後一個數據 3. at() 得到編號位置的資料 4. begin() 得到陣列頭的指標 5. end() 得
STL 中常用的 vector,map,set,sort 用法
STL中的常用的vector,map,set,sort,pair用法 C++的標準模板庫(Standard Template Library,簡稱STL)是一個容器和演算法的類庫。容器往往包
golang中sort包用法
golang中也實現了排序演算法的包sort包. sort包中實現了3種基本的排序演算法:插入排序.快排和堆排序.和其他語言中一樣,這三種方式都是不公開的,他們只在sort包內部使用.所以使用者在使用sort包進行排序時無需考慮使用那種排序方式,sort.Interface
js中sort()方法的用法,引數以及排序原理
sort() 方法用於對陣列的元素進行排序。 語法:arrayObject.sort(sortby);引數sortby可選。規定排序順序。必須是函式。 注:如果呼叫該方法時沒有使用引數,將按字母順序對陣列中的元素進行排序,說得更精確點,是按照字元編碼的順序進行排序。要實現這一點,首先應把陣列的元素都轉換成字
Python中zip()函式用法舉例
定義:zip([iterable, ...]) zip()是Python的一個內建函式,它接受一系列可迭代的物件作為引數,將物件中對應的元素打包成一個個tuple(元組),然後返回由這些 tuples組成的list(列表)。若傳入引數的長度不等,則返回list的長度和引數
C++中sort()函式用法
做專案的時候,排序是一種經常要用到的操作。如果每次都自己寫個冒泡之類的O(n^2)排序,不但程式容易超時,而且浪費寶貴的時間,還很有可能寫錯。STL裡面有個sort函式,可以直接對陣列排序,複雜度為n*log2(n)。 sort是STL中提供的演算法,標頭檔案為#inclu
Python中的集合:set與frozenset用法舉例
【1】建立: 從list或tuple中建立,我就會這兩種。不能從數字直接建立:a=set(1)錯誤! 1.sa=set(列表) 2.sa=set(tuple) 程式碼: 【2】交集 sc = sa & sb sc=sa.intersection(sb) sc=s