Enigma Enigma - 1 month ago 8
Perl Question

Perl: Printing out the file where a word occurs

I am trying to write a small program that takes from command line file(s) and prints out the number of occurrence of a word from all files and in which file it occurs. The first part, finding the number of occurrence of a word, seems to work well.

However, I am struggling with the second part, namely, finding in which file (i.e. file name) the word occurs. I am thinking of using an array that stores the word but don’t know if this is the best way, or what is the best way.
This is the code I have so far and seems to work well for the part that counts the number of times a word occurs in given file(s):

use strict;
use warnings;

my %count;

while (<>) {

my $casefoldstr = lc $_;

foreach my $str ($casefoldstr =~ /\w+/g) {
$count{$str}++;
}
}

foreach my $str (sort keys %count) {
printf "$str $count{$str}:\n";
}

Answer

Using an array seems reasonable, if you don't visit any file more than once - then you can always just check the last value stored in the array. Otherwise, use a hash.

#!/usr/bin/perl
use warnings;
use strict;

my %count;
my %in_file;
while (<>) {
    my $casefoldstr = lc;
    for my $str ($casefoldstr =~ /\w+/g) {
        ++$count{$str};
        push @{ $in_file{$str} }, $ARGV
            unless ref $in_file{$str} && $in_file{$str}[-1] eq $ARGV;
    }
}

foreach my $str (sort keys %count) {
    printf  "$str $count{$str}: @{ $in_file{$str} }\n";
}