Bonjour,
débutant en Perl, j'applique un programme assez simple qui "matche" deux listes de noms à de nouvelles listes de noms.
En gros le programme utiliser une mesure de distance entre deux mots piochés dans des listes pour assigner un score aux différentes combinaisons. Au final, il créer une table avec les propositions de matches.
Dans mon cas, j'ai une liste de district (désigné par un code numérique) et à l'intérieur de chaque district deux listes de blocks que je cherche à matcher entre eux.
Mon problème se pose en fait lors de l'application du programme à des listes de noms. Lorsque je lance le programme, Perl s'arrête au milieu de la boucle. Lorsque j'applique la boucle au seul élément qui pose problème, le programme fonctionne bien ce qui m'a conduit à conclure que ce n'est pas le programme qui ne fonctionne pas mais bien la manière dont je l'ai appliqué aux listes de noms.
J'ai l'impression que c'est lié à un problème de mémoire mais les listes sont vraiment courtes et au fond je n'en sais rien.
Merci !!
Voici le code:
C'EST DANS CETTE BOUCLE QU'APPARAIT LE PROBLEME !!
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 # Opens the list of district and blocks from the DISE_Blk05 data my $csv = Text::CSV_XS->new( {binary => 1 }); open( DISEBLK05, "<:encoding(utf8)", "data/Block05_match.csv"); #creates a hash table with every distname and blname referring to the block code in the DISE_Blk05 table my %dise05; while ($row = $csv->getline(DISEBLK05) ) { if ( $row->[0] =~ /\d{1,}/ ) { # skip first line with headers my $blkcode = "$row->[0]"; #print "$blkcode\n"; $dise05{"$row->[1], $row->[2]"} = $blkcode; } } #my @dist_ord = sort { $dise05{$a} <=> $hash{$b} } keys %hash; close(DISEBLK05); # Opens the list of district and blocks from the dise08 data my $csv = Text::CSV_XS->new( {binary => 1 }); open( DISEBLK08, "<:encoding(utf8)", "data/Block08_match.csv"); #creates another hash table with every distname and blname referring to the block code in the dise08 table my %dise08; while ($row = $csv->getline(DISEBLK08) ) { if ( $row->[0] =~ /\d{1,}/ ) { # skip first line with headers my $blkcode = "$row->[0]"; #print "$blkcode\n"; $dise08{"$row->[1], $row->[2]"} = $blkcode; } } close(DISEBLK08); foreach $key( keys %dise08) { #print "$key ---$dise08{$key} \n"; } #creates another table with distscd referring to district codes! # en même temps dans mon cas il suffit d'utilser $blkcode my %districts; foreach $district(keys %dise08) { $district =~ /^(\d+),\s\.*/; $district{$1} = $1; } my @district = keys %district; foreach $k( keys %district) { print "$k ---$district{$k}\n"; } #starts writing the matched list open(OUT, ">matched.csv"); print OUT "distcd08, blkname05, blkname08, ID05, ID08, distance\n "; my @dise05; my @dise08;
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 #loop over all district names foreach $distr(@district) { #my $distr="0203"; #when done individually, each districts works well, it doesn't seem to be a matter of format in the .csv table. print "now doing district $distr \n"; use warnings; #loop through all blocks of dise05 table and remove the district name foreach $dist_block( grep(/^$distr,\s/, keys %dise05) ) { chomp($dist_block); $dist_block =~ /^\d{1,},\s(.*)/; #print "dist 05 c'est $dist_block\n"; push(@dise05, $1); } #loop through all blocks of dise08 table and remove the district name foreach $dist_block( grep(/^$distr,\s/, keys %dise08) ) { chomp($dist_block); $dist_block =~ /^\d{1,},\s(.*)/; #print "dist 08 c'est $dist_block\n"; push(@dise08, $1); } my ($outarray1, $outarray2, $outdist) = match(\@dise08, \@dise05); my @outarray1 = @$outarray1; my @outarray2 = @$outarray2; my @outdist = @$outdist; print "@outarray1\n"; print "@outarray2\n"; $i = -1; foreach ( @outarray1 ) { $i++; my $nrega_dist_block = "$distr, @outarray1[$i]"; my $census_dist_block = "$distr, @outarray2[$i]"; print OUT "$distr, @outarray2[$i], @outarray1[$i], $dise05{$census_dist_block}, $dise08{$nrega_dist_block}, @outdist[$i]\n"; } @outarray1 = (); @outarray2 = (); @outdist = (); @dise05 = (); @dise08 = (); }
Partager