in reply to file merge problem
Since you are merging the hashes, do you really need two of them? Also, it seems that the only critical part of the line is the word at the begining, so you can just split that off separately and save the rest of the line in a single variable.
use warnings; use strict; my %filehash; my @files = qw(F1.dat F2.dat); for my $filename (@files) { open my $filehandle, '<', $filename or die "Can't open $filename $ +!\n"; while (<$filehandle>) { my ( $word, $remainder ) = split ' ', $_, 2; $filehash{$word} = $remainder; } close $filehandle; } open my $merged, '>', 'merge.dat' or die "Can't open merge.dat:$!\n"; for ( sort keys %filehash ) { print $merged "$_ $filehash{$_}"; }
Update: changed split parameter to ignore any initial whitespace
|
|---|