> Greetings, Hello > I've created a hash from an INPUT file and I'm trying > to search for each key from the hash in a DATA file. > However, I can't get my script to iteratively loop > through the DATA file for each key. Instead it loops > through DATA once for the first key. Below is some > sample data, intended output, and my code. Thank you > for any help.
Not pretty but this is how I did it. Attached are the files. #!/usr/bin/perl use strict; use warnings; my ( %genedex, %results ); open (INPUT, "<input.txt") or die ( "Could not open input.txt $!\n" ); open (DATA , "<data.txt") or die ( "Could not open data.txt $!\n" ); # Fill Genedex foreach (<INPUT>){ chomp; my ($gene_name, $remainder) = split/\t/; $genedex{$gene_name} = $remainder; } # Fill Results foreach (<DATA>){ chomp; my ( $line, @record); if ( /^(\d)\s+\(([\w\d\s]+)\)/) { $line = $1; @record = split /\s+/,$2; foreach (@record){ if ( $genedex{ $_ } ) { # not sorted # print "$_ \t $line \t\t $genedex{$_}\n"; # sorted $results{ $_ } = [ $line, $genedex{ $_ } ]; } } } } foreach (sort keys %results){ print "$_ \t $results{$_}->[0] \t\t $results{$_}->[1]\n"; }
1 (GN ID ID LKJLSKJLDK) 2 (GN ID ID5 LKJLSKJLDK) 3 (GN ID LKJLSKJLDK ID3) 4 (GN ID2 ID LKJLSKJLDK) 5 (GN ID ID9 LKJLSKJLDK) 6 (GN ID1 ID LKJLSKJLDK) 7 (GN ID ID8 LKJLSKJLDK) 8 (GN ID0 ID LKJLSKJLDK) 9 (GN ID ID4 LKJLSKJLDK)
ID1 TEST1 ID2 TEST2 ID3 TEST3 ID4 TEST4 ID5 TEST5 ID6 TEST6 ID7 TEST7
test.pl
Description: Binary data
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>