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!

In reply to Re: Problem in counting the occurrences of a string in a text file by Anonymous Monk
in thread Problem in counting the occurrences of a string in a text file by findtheriver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.