- 论坛徽章:
- 145
|
回复 1# jjwjack
$ perl fruit.pl
$ cat out.txt
apple pear banana
$ cat fruit.pl
use strict;
use warnings;
my $sFile1 = "ex1.txt";
my $sFile2 = "ex2.txt";
my $sFile_out = "out.txt";
open(my $FH1, "<", $sFile1) or die "can't open $sFile1\n";
open(my $FH2, "<", $sFile2) or die "can't open $sFile2\n";
open(my $FHout, ">", $sFile_out) or die "can't open $sFile_out\n";
my %hFruit;
# read file1 and creat hash data
while(<$FH1>){
next if(m/^\s*$/);
chomp;
my($sKey, $sFruit) = split;
$hFruit{$sKey} = $sFruit;
}
# read file2 and output to file
while(<$FH2>){
# replace the number to fruit
s/(\S+)/exists $hFruit{$1}?$hFruit{$1}:$1/ge;
print {$FHout} $_;
}
|
|