1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #!/usr/bin/perl
use strict;
use warnings;
use Sort::Fields;
use Data::Dumper;
# Construction data matrix
my @data = <DATA>;
my $t;
my $id_ligne = 0;
foreach my $ligne (@data) {
my $id_colonne = 0;
foreach my $cellule ( split( /\s+/, $ligne ) ) {
$t->{"$id_ligne,$id_colonne"} = $cellule;
$id_colonne++;
}
$id_ligne++;
}
# Pour le choix de ['2n'], lire documentation de Sort::Fields
print Dumper $t;
trier_data_matrix( $t, ['2n'], 0, 0 );
print Dumper $t;
sub trier_data_matrix {
my ( $ref_data, $ref_type_trie, $indice_row_origin, $indice_col_origin ) = @_;
# Construction tableau temporaire à trier
my @temp_data;
my $separateur = '--sep--';
my $nb_data = scalar keys %{$ref_data};
my $i = 0;
ROW:
for my $irow ( $indice_row_origin .. $nb_data ) {
COL:
for my $icol ( $indice_col_origin .. $nb_data ) {
if ( not exists $ref_data->{"$irow,$icol"} ) {
$i++;
next ROW;
}
push @{ $temp_data[$i] }, $ref_data->{"$irow,$icol"};
}
}
foreach my $ligne (@temp_data) {
$ligne = join $separateur, @{$ligne};
}
# trie
my @sorted = fieldsort $separateur, $ref_type_trie, @temp_data;
@temp_data = undef;
#print Dumper @sorted;
# Reconstruction tableau matrix
my ( $il, $ic ) = ( $indice_row_origin, $indice_col_origin );
foreach my $ligne (@sorted) {
$ic = $indice_col_origin;
foreach my $cellule ( split( $separateur, $ligne ) ) {
$ref_data->{"$il,$ic"} = $cellule;
$ic++;
}
$il++;
}
@sorted = undef;
return $ref_data;
}
__DATA__
toto 10
tata 8
tonton 12 |
Partager