关于perl中sort的用法举例

 

关于perl中sort的用法举例

 

# sort lexically-.
   @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 {4hE@
($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的聚散地  /p@twV
  @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.|

@opinions = ( 8, 9, 4, 7, 8, 5, 6, 4, 9, 9,
              7, 8, 9, 5, 4, 8, 7, 8, 7, 7,
              6, 6, 8, 9, 1, 9, 8, 7, 8, 7,
              7, 8, 9, 8, 9, 4, 9, 6, 8, 4,
              6, 7, 3, 4, 8, 7, 9, 8, 9, 2 );

# determine the mean

$total = 0;

foreach ( @opinions ) {
   $total += $_;
}

$mean = $total / @opinions;
print "Survey mean result: $mean/n";

# determine the median

@sorted = sort { $a <=> $b } @opinions;
$middle = @sorted / 2; # middle element subscript

# for an even number of elements, average the two middle

# elements to determine the median; otherwise, use the

# middle element

if ( @sorted %2 == 0 ) { # even number of elements

   $median =
      ( $sorted[ $middle - 1 ] + $sorted[ $middle ] ) / 2;
}
else { # odd number of elements

   $median = $sorted[ $middle ];
}

print "Survey median result: $median/n";

# determine the mode

$mode = 0;

foreach ( @opinions ) {
   ++$frequency[ $_ ]; # increment the frequency counter

   
   # if the current frequency is greater than the $mode's

   # frequency, change $mode to $_

   if ( $frequency[ $_ ] > $frequency[ $mode ] ) {
      $mode = $_;
   }
}

print "Survey mode result: $mode/n/n";

# display a frequency graph

print "Response/tFrequency/n";
print "--------/t---------/n";

foreach ( 1 .. 9 ) {
   print "$_/t/t", "*" x $frequency[ $_ ], "/n";
}

E7



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值