[PERL ]Tri d'un tableau 2D

Résolu
Bonjour,

J'ai un tableau Perl de deux dimensions (100 ligne * 25 colonne). Je voudrai trier mon tableau en fonction de la colonne 10.

je vous remercie d'avance pour votre aide.
Configuration: Windows XP
Firefox 3.0.1

4 réponses

  1. je vous remercie pour vos réponse.

    l'instruction suivante :
    my @tri = sort { $a->[2] <=> $b->[2] } @tab;

    marche torp bien.
    2
    1. Modérateur
      Salut,

      il suffit de trier selon le champ concerné (si nombre utilise <=> , si chaine de caractère utilise cmp )

      voici une exemple
      #!/usr/bin/perl
      #
      use warnings;use strict;
      
      my @tab = (
                 ["lami20j","x",1000,1000,"lami20j,,,","/home/lami20j","/bin/bash"],
                 ["mysql","x",104,112,"MySQL Server,,,","/var/lib/mysql","/bin/false"],
                 ["yann","x",1001,1001,"","/home/yann","/bin/bash"],
                 ["sshd","x",108,65534,"","/var/run/sshd","/usr/sbin/nologin"],
                 ["smmta","x",109,113,"Mail Transfer Agent,,,","/var/lib/sendmail","/bin/false"],
                 ["smmsp","x",110,114,"Mail Submission Program,,,","/var/lib/sendmail","/bin/false"],
                 ["bind","x",111,115,"","/var/cache/bind","/bin/false"],
                 ["postfix","x",112,117,"","/var/spool/postfix","/bin/false"],
                 ["papa","x",1002,10001,"","/home/papa","/bin/bash"],
                 ["maman","x",1003,10001,"","/home/maman","/bin/bash"],
                 ["ftp","x",113,65534,"","/home/ftp","/bin/false"],
                 ["uml-net","x",114,119,"","/home/uml-net","/bin/false"],
                 ["saned","x",115,120,"","/home/saned","/bin/false"],
                 ["c","x",1004,1004,"","/home/c","/bin/sh"],
                 ["cristi","x",1005,1005,"","/home/cristi","/bin/bash"],
                 ["invite","x",1006,1006,"","/home/invite","/bin/bash"],
                 ["clamav","x",116,121,"","/var/lib/clamav","/bin/false"],
                );
      
      my @tri = sort { $a->[2] <=> $b->[2] } @tab; # tri selon le champ 3
      print "@$_\n" for @tri;
      __END__
      
      Ce qui est intéressant c'est la possibilité de faire un tri sur un champ calculable
      Tu peux aussi utiliser plusieurs critères en cas d'égalité. Par exemple si le champ 3 est le même tu peux trier selon un autre champ
      0
      1. Modérateur
        Et le résultat
        lami20j@debian:~/trash$ perl ccm.pl
        mysql x 104 112 MySQL Server,,, /var/lib/mysql /bin/false
        sshd x 108 65534  /var/run/sshd /usr/sbin/nologin
        smmta x 109 113 Mail Transfer Agent,,, /var/lib/sendmail /bin/false
        smmsp x 110 114 Mail Submission Program,,, /var/lib/sendmail /bin/false
        bind x 111 115  /var/cache/bind /bin/false
        postfix x 112 117  /var/spool/postfix /bin/false
        ftp x 113 65534  /home/ftp /bin/false
        uml-net x 114 119  /home/uml-net /bin/false
        saned x 115 120  /home/saned /bin/false
        clamav x 116 121  /var/lib/clamav /bin/false
        lami20j x 1000 1000 lami20j,,, /home/lami20j /bin/bash
        yann x 1001 1001  /home/yann /bin/bash
        papa x 1002 10001  /home/papa /bin/bash
        maman x 1003 10001  /home/maman /bin/bash
        c x 1004 1004  /home/c /bin/sh
        cristi x 1005 1005  /home/cristi /bin/bash
        invite x 1006 1006  /home/invite /bin/bash
        
        0

        Discussions similaires

        Code Ascii

        14 réponses