求排序问题代码

求排序问题代码

求排序问题代码
各位大虾,小弟在这想请教个文件排序问题
我有一文件 fileA具体内容含如下三行
"NM_183380.1|,2145,3962,1843,8,8,29,75":1823
"NM_183380|,2145,3962,1843,8,8,29,75":1843
"NM_002745.4|,1244,2756,1537,9,8,31,67":1537

我希望按每行冒号后的数值排序,(升序)得到fileB文件如下的显示
"NM_002745.4|,1244,2756,1537,9,8,31,67":1537
"NM_183380.1|,2145,3962,1843,8,8,29,75":1823
"NM_183380|,2145,3962,1843,8,8,29,75":1843

不知哪位大虾能方便用PERL给小弟写个排序代码,不胜感激^-^

perl codes:--[quote] perl codes:
[quote]
# usage: perl $0 fileA

use strict;
use warnings;

my %hash;
while (<>) {
chomp;
my ($v,$k) = split ':',$_;
$hash{$k} = $v;
}

my @sorted = map{ {($_ =>$hash{$_})} } sort keys %hash;

foreach my$hashref (@sorted) {
my ($key, $value) = each %$hashref;
print "$value:$key\n";
}
[/quote]

Shell:
[quote]sort -t":" -k2 fileA[/quote]
[CCB]1[/CCB]非常感谢哦.
[CCB]1[/CCB]非常感谢哦
恩,还是用shell爽。。。。
不用shell直接数值排序
#!/usr/bin/perl -w

use strict;
use warnings;

my $inFile = "I:\\Perl\\fileA.txt";  # input file's name, you should adapt it by yourself
my $outFile = "I:\\Perl\\fileB.txt"; # output file's name, you should adapt it by yourself

open IN, $inFile || die "No such files!\n";
open OUT, ">".$outFile || die "Couldn't create this file!\n";

my %data;
while (<IN>) {
     chomp;
     my($v, $k) = split /:/, $_;
     $data{$k} = $v;
}

close IN;

my @keys = sort {$a<=>$b} (keys %data); # sort

foreach (@keys) {
     print OUT "$data{$_}:$_\n";
}

close OUT;
先把“:”换成"\t",然后拿到excel中去排序,排好后再把"\t"换成冒号就行了
呵呵,楼上的办法聪明,我.
呵呵,楼上的办法聪明,我会把“:”换成逗号,然后文件改存成CSV格式,然后再排序。
#!/usr/bin/perl -w--o.
#!/usr/bin/perl -w
open (file1,"<D:\\zhu-PL\\zhu.txt");
open (file3,">D:\\zhu-PL\\zhu4.txt");
my %name;
$n=0;
while(<file1>)
{
chomp;
my ($v,$k) = split ':',$_;
$name{$k} = $v;
$v_a[$n]=$k;
$n+=1;
}
@v_a=sort{$a<=>$b}@v_a;
for($id=0;$id<$n;$id++)
{
printf(file3 "$name{$v_a[$id]}:$v_a[$id]\n");
}
close file1;
close file3;
print "Press <Enter> to continue...";
<STDIN>;
#这是小弟编的,请各位大侠指教!!!
#!/usr/bin/perl -w--o.
#!/usr/bin/perl -w
open (file1,"<D:\\zhu-PL\\zhu.txt");
open (file3,">D:\\zhu-PL\\zhu4.txt");
my %name;
$n=0;
while(<file1>)
{
chomp;
my ($v,$k) = split ':',$_;
$name{$k} = $v;
$K[$n]=$k;
$n+=1;
}
@K=sort{$a<=>$b}@K;
$n=0;
while(defined($K[$n]))
{
printf(file3 "$name{$K[$n]}:$K[$n]\n");
$n++;
}
close file1;
close file3;
print "Press <Enter> to continue...";
<STDIN>;
#小弟发现刚才的,多拉几个变量,又改了改。