我有两个文件。一个文件只包含键,另一个文件同时具有键和值。我如何将一个文件的键与另一个文件的值进行比较?
example of file1
steve
robert
sandy
alex
example of file2
age25, steve
age29, alex
age30, mindy
age50, rokuna
age25, steve
example of output
age25, steve
age29, alex
这是我到目前为止所拥有的
my $age_name="file1.txt";
my $name="file2.txt";
open my $MYFILE, "<", $name or die "could not open $name \n";
open my $MYFILE2, "<", $age_name or die "could not open $age_name \n";
while(<$MYFILE>) {
my ($key, $value) = split(",");
my $secondfile = <$MYFILE2>;
if ( defined $secondfile ) {
my ($key2, $value2) = split(",");
if ($value2=~m/$key/) {
print "$key2 - $value2 \n";
}
}
}
close $MYFILE;
close $MYFILE2;