tux242,
As I explained in the CB, I understand how frustrating it can be just getting started. If you can put in clear and concise terms what you are trying to accomplish, we will be much more able to help you. What I discerned from the CB is this:
"You are trying to get a list of items that appears in both file1 and file2, concatenate them together, and disregard the rest".
#!/usr/bin/perl -w
use strict;
my %data;
my @files = qw(file1.dat file2.dat);
for my $file ( @files ) {
parse_file( $file, \%data );
}
for my $key ( grep $data{$_}->[1] > 1 , keys %data ) {
print join ':' , $key , $data{$key}->[0];
}
sub parse_file {
my ($file, $data) = @_;
open (INPUT, $file) or die "Unable to open $file : $!";
while ( <INPUT> ) {
my ($key, $value) = split /:/ , $_ , 2;
$data->{$key}[0] = $value;
$data->{$key}[1]++;
}
}
Tailor as needed.
Explanation:
Use a single hash of Arrays (HoA) for any number of files.
Each hash key points to an array with two indices
The first index (0) is the expected value
The second index (1) is how many times this key has been seen
Only pull out keys were the second index has been seen more than once
Of course there are some caveats such as only the last seen value of a key will get used.
Cheers - L~R
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.