Hello again mao9856,
Not knowing it is not a problem, nobody started coding and knew everything. This forum is open and free for people to learn and contribute. You provide us a bit of a non working code. You either got it from someone or you modified it to the point it was not working. It is good for you and all of us to practice and try to provide a working sample of code that shows what you have tried and where you got stuck. For us to provide you a solution it is really easy but it would not mean that it resolves your problem since you will not learn anything out of it.
Having said that here is a sample of code that it does what you want.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::MoreUtils 'frequency';
my (@lines);
my $numberOfFiles = scalar @ARGV;
while (<>) {
next if /^\s*$/; # skip empty lines (remove if not needed)
chomp;
push @lines, $_;
} continue {
close ARGV if eof; # Not eof()!
}
my @frequencyLines = frequency @lines;
my %frequencyHash = @frequencyLines;
my @unwanted;
foreach my $key (keys %frequencyHash) {
if ($frequencyHash{$key} != $numberOfFiles) {
push @unwanted, $key;
# push any related keys onto @unwanted
}
}
delete @frequencyHash{@unwanted};
my @matches = keys %frequencyHash;
print Dumper \@matches;
__END__
$ perl test.pl File1.txt File2.txt File3.txt
$VAR1 = [
'ID122 EFG87',
'ID121 ABC14',
'ID157 TSR11'
];
I used as input files the first 3 files (File 1 -3) as input DATA that you provided us.
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|