use File::Slurp 'read_file';
my @data = map { [ read_file( $_, chomp => 1 ) ] } 'file1', 'file2';
print "$data[0][$_] = $data[1][$_]\n" for 0 .. $#{$data[0]};
perlintro might give you some ideas on how to accomplish it in a more straightforward way. The steps would probably include the following:
- Open both input files with unique filehandles.
- Using a while loop, iterate over the file that contains identifiers.
- Inside the loop, read a line from the file that contains values.
- Chomp the identifier line.
- Print the current identifier, and the current value, with a " = " between them.
- Continue to the next iteration.
Update:
In place of the original solution I posted, this one takes into consideration blank lines, and a trailing newline at the end of the file:
my @data = map { [ read_file( $_, chomp => 1 ) ] } 'file1', 'file2';
print map {
length $data[0][$_] ? "$data[0][$_] = $data[1][$_]\n" : ''
} 0 .. $#{$data[0]};
...but that starts looking ugly, so here's another alternative:
use File::Slurp 'read_file';
use List::MoreUtils 'pairwise';
print
&pairwise( sub { length $a ? "$a = $b\n" : () },
map { [read_file($_,chomp=>1)] } 'file1', 'file2' );
Ultimately all of these probably sacrifice legibility and familiarity in favor of being clever, and that's usually not a good approach. My suggestion is to stick to the steps I outlined in the bullet points.
|