in reply to Help with pushing into a hash
Here's an option to consider:
use Modern::Perl; use File::Slurp qw/read_file write_file/; my $test = 'test.txt'; my $test2 = 'test2.txt'; my $activout = 'ActivACNPF.txt'; my @lines; my %data = map { /(.+)\s+\|\s+(.+)/; $1 => $2 } read_file $test2; for ( read_file $test ) { /(.+)\s+.+=([^\s]+)/; push @lines, "$1 $2 $data{$1}\n" if $data{$1}; } write_file $activout, @lines;
Output to file:
Q197F8 IIV3-002R PF04947.9 Q91G88 IIV6-006L PF01486.12 PF00319.13
%data is initialized using the captured data from test2.txt as key/value pairs. Next, the 'keys' and associated 'values' are captured from test.txt, and the completed line is push onto @lines if a matching key is found. Finally, @lines is written to ActivACNPF.txt.
Hope this helps!
Update: Replaced a single-line map with a multi-line for to improve readability.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help with pushing into a hash
by jemswira (Novice) on Aug 30, 2012 at 11:32 UTC | |
by Kenosis (Priest) on Aug 30, 2012 at 17:16 UTC | |
by jemswira (Novice) on Aug 31, 2012 at 11:48 UTC | |
by Kenosis (Priest) on Aug 31, 2012 at 15:16 UTC | |
by jemswira (Novice) on Aug 31, 2012 at 15:43 UTC | |
| |
by Anonymous Monk on Aug 30, 2012 at 11:39 UTC | |
by jemswira (Novice) on Aug 30, 2012 at 11:52 UTC | |
by Anonymous Monk on Aug 30, 2012 at 12:29 UTC | |
|
990972
by jemswira (Novice) on Aug 31, 2012 at 10:30 UTC |