Since you only want the ones that are contained in both files, you only need one hash, containing a hash or an array (your choice, really) for each infinitive found. Then go through the hash and output all those that have both parts filled.
#!/usr/bin/perl my %k; # hash to hold infinitives and their other parts process( 'thirdperson', 'file1'); process( 'past', 'file2'); for (keys %k){ if( $k{$_}{thirdperson} and $k{$_}{past} ){ print "$_\t$k{$_}{thirdperson}\t$k{$_}{past}\n"; } } sub process { my $part = shift; my $filename = shift; open my $fn, '<', $filename or die $!; while(<$fn>){ chomp; my($other, $infinitive) = split /\t/; $k{$infinitive}{$part} = $other; } close $fn; }
I used a hash of hashes, because I thought that would make what I was doing clearer. Each hash within %k is only printed if it contains values for both keys 'thirdperson' and 'past'. But you could also use a two-element array, as long as you keep track of which form of the verb goes in which numbered element.
Edited to add: The unix command 'join' also does this, printing lines from multiple files that share a common field. Both files have to be sorted first by the field being compared, though.
sort -k2 -o file1 file1 sort -k2 -o file2 file2 join -j 2 -t ' ' file1 file2 # inserted a tab between the quote +s with Ctrl-V
In reply to Re: script to merge files
by aaron_baugher
in thread script to merge files
by marylein
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |