- 论坛徽章:
- 6
|
这样? - #!perl
- use strict;
- use autodie qw{open close};
- my $file1 = 'file1.txt';
- my $file2 = 'file2.txt';
- # read data
- my %weight_of = ();
- open my $fh2, '<', $file2;
- while ( defined(my $line = readline $fh2) ) {
- chomp $line;
- my @parts = split /\s+/, $line;
- my $char = $parts[0];
- my $weight = $parts[1];
- $weight_of{$char} = $weight;
- }
- close $fh2;
- # again...
- my $data_href = +{};
- open my $fh1, '<', $file1;
- while ( defined(my $line = readline $fh1) ) {
- chomp $line;
- my @parts = split /\s+/, $line;
- my $class = $parts[0];
- my $char = $parts[1];
- $data_href->{$class}->{$char} = $line;
- }
- close $fh1;
- # sort
- for my $class (sort keys %{ $data_href }){
- my @sorted_chars = sort {
- $weight_of{$b} <=> $weight_of{$a}
- } keys %{ $data_href->{$class} };
- my @lines = map { $data_href->{$class}->{$_} } @sorted_chars;
- # print sorted lines
- print "$_$/" for @lines;
- }
- __END__
复制代码 |
|