Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

I open a file and place all its contents into an array. I want to check if a particular string occurs a certain number of times in the array. Is there a way I can do this using the (=~ m / / )? I know how to do it in grep. I was looking for a way to do it with the (=~ m//) method.

Thanks

  • Comment on find a string a certain number of times

Replies are listed 'Best First'.
Re: find a string a certain number of times
by Tanktalus (Canon) on Jan 29, 2005 at 15:04 UTC

    If you have an array, @file, then you have to loop through it. m// cannot loop through an array in perl5 (maybe perl 6?). However, if you put the entire file into a scalar, now we can get somewhere:

    my $file = join '', @file; # put it all in a single scalar if ($file =~ m/(?:what you want to find.*){6}/) { print "'what you want to find' was in the file at least 6 times.\n"; }
    That said, grep would use up less space, which may be important for large files. The reason is because you're duplicating the file in both an array and a scalar. The obvious way to overcome that is to put it in the scalar to begin with - which means you don't have an array later if you want to look up lines in an array.

    Of course, for really large files, you'll want to iterate through the file one line at a time to keep from loading 100MB of text into memory at once:

    my $count; while (<$fh>) { $count++ while /what you want to look for/g; if ($count >= 6) { last; } } $fh->close(); if ($count >= 6) { print "found 'what you want to look for' at least 6 times in the fil +e.\n"; }
    And, of course, this means you don't have the file in memory at all when you're done, which may not be what you want either.

Re: find a string a certain number of times
by gaal (Parson) on Jan 29, 2005 at 15:25 UTC
    $matches += () = m/\Q$a_particular_string\E/g

    Repeat for each member of the array.

Re: find a string a certain number of times
by foil (Sexton) on Jan 29, 2005 at 21:14 UTC
    I think this is better for beginners :
    use strict; my $count; my @array; open FILE, "filename"; while (<FILE>){ push @array, $_; } foreach $a(@array){ if ($a =~ m/whatever/ ) { $count ++; } } print $count,"\n";
    basic perl stuff ... no need to get crazy ... yes , you do not need the array, if you are only counting. Your regular exp may need some fussing, but that's the fun. Cheers
      while (<FILE>){ push @array, $_; }
      is generally written
      @array = <FILE>;

      Caution: Contents may have been coded under pressure.
Re: find a string a certain number of times
by AJRod (Scribe) on Jan 30, 2005 at 01:02 UTC
    It depends on what you mean by checking "if a particular string occurs a certain number of times in the array":

    1. You may want to count the absolute number of occurences of the string in the array. Meaning, you count three if array element 52 contains three occurences of the string. In this case, you use the suggestion of gaal above.

    2. On the other hand, you may want to count the number of array elements which test positive on the occurence of the string, regardless of how many strings each array element contains. Meaning, you count only once even if array element 52 contains three occurences of the string. In this case, you use the suggestion of foil above.