in reply to Problem in counting the occurrences of a string in a text file

I tried following your suggestions and working on the code, but I am not capable of make it work.
What I'd like to do is count every occurrence of the given relation (con/con/E is a relation) with every couple of work I have. If in my file I have 3 occurrences of the string
computer/computer/S  .* con/con/E   .* processore/processore/S
I'd like for my output to be like this:
computer-processore)-->3
The new code I wrote is this one (I tried to make it more readable, and, by the way, I also fixed the fact that my input file had some line with just * and some other with .*) :
#!/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;

Problem is, when I get the output, is a nice list and all, but it's not possible that every couple and relation has exactly the same number of occurrences. And that is exactly what I get, like this:
([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
It says 27 even for couple that never appear in the same string with the given relation.
Thanks everyone for the help! Every suggestion is very well appreciated!

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

    Did you see and consider my reply (though its based upon u67129's code)?

      No. I just saw and tried it. It works now, perfectly fine. Thank you so much! You have no idea how much I appreciate it!