求一算法,



QUOTE:
原帖由 cfk 于 2007-11-22 12:40 发表


恩, 要明白, 有人就是比较笨啊 ...

哈哈,真会开玩笑,原来倒是看过最后排序的时候用负号
没想到在hash存储的时候,也用负值,哈哈,学习了


QUOTE:
原帖由 sharmy1220 于 2007-11-21 13:52 发表
perl 里面
$temp = "a_c_d_中_e_中_f_f_中_f_j_中_a_c_中_d_e_中_f_a_f_e_c_a_d_a_a";(_)只是个分隔符。
这个字符串里面通过什么样的办法,我想要实现对temp的字符进行统计出现的次数同时进行排序最多次的放 ...

Hi,

Using split(), map{}, hash and sort hash's value can resolve your problem:
For example:

QUOTE:
$temp = "a_c_d_中_e_中_f_f_中_f_j_中_a_c_中_d_e_中_f_a_f_e_c_a_d_a_a";
# here split(..) returns a list
# map generate a hash, key is 字符 and value is 统计出现的次数
%count = map { $_ => 1+ $count{$_}++ } split('_', $temp);

# output:
# sort hash's value descending
foreach my $key (sort {$count{$b}<=>$coutn{$a}} keys %count) {
    print "$key : $count{$key} \n"
}

--ulmer

谢谢各位大侠!
my $temp = "a_c_e_g_d_e_f_g";
my %h;

foreach (split(/_/, $temp))
{
    $h{$_}--;
}

foreach (sort {$h{$a} <=> $h{$b}} (keys %h))
{
        print "$_: ", -$h{$_}, "\n";
}


[Copy to clipboard] [ - ]
CODE:
$temp = "a_c_d_中_e_中_f_f_中_f_j_中_a_c_中_d_e_中_f_a_f_e_c_a_d_a_a";
map{$hh{$_}++}sort split/_/,$temp;
map{print "$_  $hh{$_}\n"}sort{$hh{$a} <=> $hh{$b}}keys %hh;