http://www.perlmonks.org?node_id=801933


in reply to ISOLATE 2 ASSOCIATED FIELDS IN A TEXT FILE, then CONVERT the first into another based on a table of definitions

From how I understand your question, this should help you get started. Note that there are many ways of doing this.
#!/usr/bin/perl use strict; use Data::Dumper; my %temp_record; my $file_1 =<<FILE_1; 385#19126!NM_167210@[1103;1104] 2 386#19127!NM_167211@[1103;1104] 2 387#19128!NM_167212@[1103;1104] 2 438#1781!NM_135492@[1337] 1 442#1794!NM_001042886@[1349] 1 FILE_1 open( my $fh, '<', \$file_1 ) or die( "Cannot open $file_1" ); while (<$fh>) { my ($key, $value) = /\!(\w+)\@\S.*\s+(\d)/; $temp_record{$key} = $value; } close( $fh ); my %record; while (<DATA>) { my ($key, $value) = /(\S+)\s+(\S+)/; for my $i (keys %temp_record) { $record{$key} = $temp_record{$i} if ( $i eq $value ); } } print Dumper \%record; __DATA__ CG32694-RD NM_167211 CG32694-RC NM_167210 CG32694-RA NM_167209 CG32694-RB NM_167212 CG33557-RA NM_001014730
The output is:
$VAR1 = { 'CG32694-RD' => '2', 'CG32694-RB' => '2', 'CG32694-RC' => '2' };