in reply to Problem in counting the occurrences of a string in a text file
#!/usr/bin/perl use strict; use warnings; open my $listaParole,"File_Input/Coppie_Parole.txt" or die; my %hash; while (my $line=<$listaParole>) { chomp $line; my ($word1, $word2) = split /:/, $line; $hash{$word1} = $word2; } close $listaParole; open my $testo, "<Wiki_Pulito/Prova/Pattern2.txt" or die; open my $conteggio, ">Wiki_Pulito/Prova/Conteggio1.txt" or die; my $count=0; my %arrayris; while (my $text=<$testo>){ for my $key (keys %hash){ my $value = $hash{$key}; if ($text =~/($key\/$key\/S)\s{0,4}(\.\*)\s{0,4}(con\/con\/E)\s{0 +,4}(\.\*)\s{0,4}($value\/$value\/S)\b/g){ $count++; } my $arrkey=$key."-".$value; $arrayris{$arrkey}=$count; } } while ( my ($k,$v) = each %arrayris ) { print $conteggio "($k) => $v\n"; } close $testo; close $conteggio;
It says 27 even for couple that never appear in the same string with the given relation.([aA]mplificator[ei]-[Tt]ransistor) => 27 ([cC]ervello-[Tt]alamo) => 27 ([Ee]ucariot[ia]-[Mm]embran[ae]) => 27 ([Cc]erio-[Ii]sotop[oi]) => 27 ([Cc]ellul[ae]-[Nn]ucle[oi]) => 27 ([Tt]ronco-[Tt]orace) => 27 ([Bb]raccio-[Aa]vambraccio) => 27
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem in counting the occurrences of a string in a text file
by linuxer (Curate) on Dec 29, 2008 at 21:51 UTC | |
by Anonymous Monk on Dec 29, 2008 at 22:00 UTC |