Here is a way to do it using two hashes, %n_count to keep track of the number of times each name appears, and %f_count to keep track of the files in which each occurs.
my (%f_count,%n_count);
for my $file (@files) {
open( my $fh, '<', $file ) or die "Couldn't read '$file': $!";
while (my $line = <$fh>) {
chomp $line;
next unless $line;
my ($name,$number) = split(',',$line);
$n_count{$name}++;
$f_count{$name}{$file}++;
}
}
Then you need to extract the names - first get the names that appear at least 25 times in the %n_count hash, then, for each of those candidate names, get the ones that appear in all files.
my $num_of_files = scalar @files;
my $min = 25;
my @candidates = grep { $n_count{$_} >= $min } keys %n_count;
for my $name (@candidates) {
my $in_files = scalar keys %{ $f_count{$name} };
next unless $in_files == $num_of_files;
print "$name\n";
}
The only tricky bit here is the
scalar keys %{ $f_count{$name} }$f_count{$name} is a hash reference, where each key is a file name. We can get at the keys by dereferencing the hash
%{...} and counting how many there are. If that count equals the number of files then that name is in every file.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.