Hi,
It would help if you presented less data and your examples reflected the data you are using.

Some initial thoughts:
1) The while loop around the Regex will be infinite if a match is found. I think that should read
$conto++ if $value =~/(($key\/$key\/S)\s{0,2}(\.\*)\s{0,2}(con\/con\/E)\s{0,2}(\.\*)\s{0,2}($value\/$value\/S))/is){
2) The while, open & close statements can be improved see below 3) You are inconsistent with your die and close statements, perhaps because you haven't got round to tidying them up yet
4) The con\/con\/E part of the Regex probably needs to be in a variable so you can loop through the other possibilities e.g. "dalla/da/E"
5) As your Regex is ignoring case the e.g. Nnoption is redundant.

The following code seems to work and incorporates some of the above points. I've also simplified the Regex as the example data works with this Regex.
#!/usr/bin/perl use strict; use warnings; open( INPUT, "<Wiki_Pulito/Prova/Pattern2.txt") or die "Can't open Pat +tern2.txt"; open( LISTAPAROLE,"<File_Input/Coppie_Parole.txt") or die "Can't open +Coppie_Parole.txt"; my %hash; while (<INPUT>) { chomp; my ($word1, $word2) = split /:/, $_; $hash{$word1} = $word2; } close INPUT; # Carico la parte di file di testo che va analizzata open( CONTEGGIO, ">Wiki_Pulito/Prova/Conteggio.txt") or die "Can't ope +n Conteggio.txt"; # Apro il file di output my $conto=0; my %arrayris; while (my $text = <LISTAPAROLE>){ for my $key (keys %hash){ my $value = $hash{$key}; if ($text =~/$key\/$key\/.*con\/con\/E.*$value\/$value +\/S/is){ $conto++; } my $arrkey=$key."-".$value; $arrayris{$arrkey}=$conto; } } while ( my ($k,$v) = each %arrayris ) { print CONTEGGIO "($k) => $v\n"; } close LISTAPAROLE; close CONTEGGIO;

In reply to Re: Problem in counting the occurrences of a string in a text file by u671296
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.