in reply to Compare two file text input, compare it, replace and write new file
open (DFILE,$dfile) or die "can not open"; open (SFILE,$sfile) or die "can not open"; open (TFILE,"> $tfile") or die "can not open";
That would be better as:
open DFILE, '<', $dfile or die "can not open '$dfile' because: $!"; open SFILE, '<', $sfile or die "can not open '$sfile' because: $!"; open TFILE, '>', $tfile or die "can not open '$tfile' because: $!";
$words{$key} .= exists $words{$key} ? "$val" : $val;
That would be better as:
$words{$key} .= $val;
while (my $s = <SFILE>) { chomp($s); my @words = split / /, $s;
That would be better as:
while ( <SFILE> ) { my @words = split;
for my $i (0 .. $#words) { $words[$i] = $words{$words[$i]} if (exists($words{$words[$i]})) }
That would be better as:
for my $word ( @words ) { $word = $words{ $word } if exists $words{ $word }; }
print TFILE join(' ', @words),$/; print TFILE "<br>";
That would be better as:
print TFILE "@words\n<br>";
The code above work if only if the source.txt contain keys separated with space,
\ca \ra \ka \tlbut fail to work if source.txt is
\ca\ra\ka\tl
$ perl -le'$_ = q/\ca\ra\ka\tl/; print; print for split /(?=\\)/' \ca\ra\ka\tl \ca \ra \ka \tl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Compare two file text input, compare it, replace and write new file
by wa2nlinux (Novice) on Feb 15, 2012 at 01:55 UTC | |
by wa2nlinux (Novice) on Feb 15, 2012 at 13:12 UTC |