IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

séparateur sans utiliser split


Sujet :

Langage Perl

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 142
    Points : 57
    Points
    57
    Par défaut séparateur sans utiliser split
    Bonsoir,

    J'ai des chaine de type variable, mais la structure étant toujours alternance (chiffre puis lettre), la taille de la strucure est différente et les lettres ne sont pas dans le même ordre
    63M1D37M1H
    1H98M2H
    1H100M

    J'aimerais pour chaque
    comptabiliser les chiffres (63+1+37+1) et avoir la correspondance M=100(63+37) D= 1 H=1
    63M1D37M1H : 102 ; M=100 D=1 H=1
    1H98M2H : 101 ; M=98 H=3(2+1)
    1H100M : 101 ; M=100 H=1

    mais voilà je ne peux pas utiliser split car je n'ai pas de motif/séparateur.


    $mot=63M1D37M1H;
    @test=split /[A-Z]/, $mot;

    Quelqu'un aurait une idée sur le type d'algo?
    Merci

  2. #2
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #!/usr/bin/perl
    use Carp;
    use strict;
    use warnings;
     
    my %mots;
    while ( my $ligne = <DATA> ) {
      chomp $ligne;
     
      my %comptage;
      my $total = 0;
      my @res = $ligne =~ m{(\d+)([a-z]+)}gi;
      for ( my $i = 0; $i < scalar(@res) -1; $i = $i +2 ) {
        if ( $res[$i+1] ) {
          $comptage{ $res[$i+1] } += $res[$i];
          $total += $res[$i];
        }
      }
     
      # Affichage
      print "===================\n$ligne : $total ;\n";
      foreach ( sort keys %comptage ) {
        print "$_ => $comptage{$_}\n";
      }
    }
     
    __DATA__
    63M1D37M1H
    1H98M2H
    1H100M
    ===================
    63M1D37M1H : 102 ;
    D => 1
    H => 1
    M => 100
    ===================
    1H98M2H : 101 ;
    H => 3
    M => 98
    ===================
    1H100M : 101 ;
    H => 1
    M => 100

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 142
    Points : 57
    Points
    57
    Par défaut
    Merci Djibril,

    -DATA-
    papa 63M1D37M1H
    mama 1H98M2H
    lala 1H100M
    toto 63M5D37M1H


    J'obtiens différents messages que je n'arrive pas à corriger (j'ai initialisé mes variables)
    Use of uninitialized value $id in hash element at xxx line 13, <> line 5.
    Use of uninitialized value $id in hash element at xxxl line 14, <> line 5.
    Use of uninitialized value in pattern match (m//) at xxxl line 14, <> line 5.
    Use of uninitialized value $id in hash element at xxx line 35, <> line 5.
    Use of uninitialized value in concatenation (.) or string at xxx line 39, <> line 5.
    Use of uninitialized value in concatenation (.) or string at xxx line 39, <> line 5.
    Use of uninitialized value in concatenation (.) or string at xxx line 39, <> line 5.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #!/usr/bin/perl
    use strict;
    use warnings;
     
     
    my %hash;
     
    while( my $ligne = <> ) {
      	chomp $ligne;
     
    	my ($id, $mots) = split /\s+/, $ligne;
     
    	$hash{$id}{'code_mots'} = $mots;
    	my @res = $hash{$id}{'code_mots'} =~ /(\d+)([a-z]+)/gi;
     
    	my $total = 0;
    	my %comptage;
     
    	for ( my $i = 0; $i < scalar(@res) -1; $i = $i +2 ) {
     
    		if ( $res[$i+1] ) {
    			$comptage{$res[$i+1] } += $res[$i];
    			$total += $res[$i];
     
    			if( $res[$i+1]  eq "M"){
    				$hash{$id}{'M'}= $comptage{$res[$i+1]};
     
    			}	
    			if( $res[$i+1]  eq "D"){
    				#my $nbre = {'D' => $comptage{$res[$i+1] }};
    				$hash{$id}{'D'}=$comptage{$res[$i+1]} ;
    			}
    		}
    	}
    	$hash{$id}{'taille'} = $total;
    }

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 142
    Points : 57
    Points
    57
    Par défaut
    au lieu de split j'ai fait une expression régulière

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #!/usr/bin/perl
    use strict;
    use warnings;
     
     
    my %hash;
     
    my $id;
     
    while( my $ligne = <> ) {
      	chomp $ligne;
     
    	#my ($id, $mots) = split /\s+/, $ligne;
     
    	if($ligne =~ /(.+)\t(.+)$/){
    		$id=$1;
    		my $mots=$2;
     
    		$hash{$id}{'code_mots'} = $mots;
    		my @res = $hash{$id}{'code_mots'} =~ /(\d+)([a-z]+)/gi;
     
    		my $total = 0;
    		my %comptage;
     
    		for ( my $i = 0; $i < scalar(@res) -1; $i = $i +2 ) {
     
    			if ( $res[$i+1] ) {
    				$comptage{$res[$i+1]} += $res[$i];
    				$total += $res[$i];
     
    				if( $res[$i+1]  eq "M"){
    					$hash{$id}{'M'} = $comptage{$res[$i+1]};
     
    				}	
    				if( $res[$i+1]  eq "I"){
    					$hash{$id}{'I'} = $comptage{$res[$i+1]} ;
    				}
    				if( $res[$i+1]  eq "D"){
    					$hash{$id}{'D'} = $comptage{$res[$i+1]} ;
    				}
    			}
    		}
    		$hash{$id}{'taille'} = $total;
    	}
    }

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 142
    Points : 57
    Points
    57
    Par défaut
    Bonjour,

    Le script qui fonctionne mais il a un message d'erreur que je ne comprend pas :
    Use of uninitialized value in concatenation (.) or string at xxxl line 57, <> line 1.

    Use of uninitialized value in addition (+) at xxx line 70, <> line 7.

    voiçi ma ligne 57
    print "M: $hash{$id}{'M'}\tI: $hash{$id}{'I'}\tH: $hash{$id}{'H'}\tD: $hash{$id}{'D'}\n";#verif

    et ligne 70
    $hash{$_}{'indel'} = $hash{$_}{'I'} + $hash{$_}{'D'};



    Je comprend que c'est parce que mes variables sont vides, mais j'ai bien fait le test si ma variable est définie
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if ( $res[$i+1] ) {# vrai si $res[$i+1] est indéfini ou si $res[$i+1] est faux
    ...
    }
    par exemple pour b 1H98M2H, j'ai pas de valeur pour I et D
    M: 98 I: H: 3 D:

    -DATA-
    b 1H98M2H
    c 1H100M
    d 63M5D37M1H
    f 1H68M3D31M1H
    g 1H75M1I24M
    h 1H38M1I60M1H


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #!/usr/bin/perl
    use strict;
    use warnings;
     
     
    my %hash;
     
    my $id;
     
    while( my $ligne = <> ) {
      	chomp $ligne;
     
    	#my ($id, $mots) = split /\s+/, $ligne;
     
    	if($ligne =~ /(.+)\t(.+)$/){
    		$id=$1;
    		my $mots=$2;
     
    		$hash{$id}{'code_mots'} = $mots;
    		my @res = $hash{$id}{'code_mots'} =~ /(\d+)([a-z]+)/gi;
     
    		my %comptage;
     
    		my $nb_M;
    		my $nb_I;
    		my $nb_H;
    		my $nb_D;
     
    		for ( my $i = 0; $i < scalar(@res) -1; $i = $i +2 ) {
     
    			if ( $res[$i+1] ) {
    				$comptage{$res[$i+1]} += $res[$i];
    				#$total += $res[$i];
     
    				if( $res[$i+1]  eq "M"){
    					$hash{$id}{'M'} = $comptage{$res[$i+1]};
    					$nb_M += $comptage{$res[$i+1]};
    				}	
    				if( $res[$i+1]  eq "I"){
    					$hash{$id}{'I'} = $comptage{$res[$i+1]} ;
    					$nb_I += $comptage{$res[$i+1]};
    				}
    				if( $res[$i+1]  eq "H"){
    					$hash{$id}{'H'} = $comptage{$res[$i+1]} ;
    					$nb_H += $comptage{$res[$i+1]};
    				}
    				if( $res[$i+1]  eq "D"){
    					$hash{$id}{'D'} = $comptage{$res[$i+1]} ;
    					$nb_D += $comptage{$res[$i+1]};
    				}
    			}
     
    		}
    		print "M: $hash{$id}{'M'}\tI: $hash{$id}{'I'}\tH: $hash{$id}{'H'}\tD: $hash{$id}{'D'}\n";
    	}
    }
     
     
    foreach (  sort keys %hash ) {
    	$hash{$_}{'del'} = $hash{$_}{'I'} + $hash{$_}{'D'};
    }

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    142
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 142
    Points : 57
    Points
    57
    Par défaut
    j'ai crée un nouveau post cf. Use of uninitialized value in concatenation (.) or string

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. utiliser split() avec un / (comme séparateur)
    Par skyangel20 dans le forum Langage
    Réponses: 2
    Dernier message: 24/05/2007, 20h47
  2. [CR8.5] Image dynamique sans utiliser RDC ou Blob
    Par lrp dans le forum SAP Crystal Reports
    Réponses: 2
    Dernier message: 21/12/2005, 14h43
  3. Réponses: 6
    Dernier message: 27/05/2004, 10h41
  4. [][Timer] Créer un Timer sans utiliser le composant
    Par HPJ dans le forum VB 6 et antérieur
    Réponses: 8
    Dernier message: 01/10/2003, 11h04
  5. Tore en OpenGL sans utiliser glut
    Par lefort dans le forum Algorithmes et structures de données
    Réponses: 6
    Dernier message: 20/11/2002, 16h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo