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.
In reply to Re: Help with pushing into a hash
by Kenosis
in thread Help with pushing into a hash
by jemswira
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |