On Jan 2, 2004, at 9:33 AM, David Byrne wrote:

Greetings,

Howdy.


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.

I'm a little fuzzy on this part, but will see if I can figure it out.


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.

Well, if you want to go back over the DATA for each gene, you need to reopen that file each time to start over.


David

INPUT...
ID1     TEST1
ID2     TEST2
ID3     TEST3
ID4     TEST4
ID5     TEST5
ID6     TEST6
ID7     TEST7

DATA...
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)

OUTPUT is:
ID1     6       TEST1

OUTPUT should be:
ID1     6       TEST1
ID2     4       TEST2
ID3     3       TEST3
ID4     9       TEST4
ID5     2       TEST5

MY CODE...

I suggest adding:


use strict;
use warnings;

Write good code, stay sane longer and get better help from us. Win, win, win.

I assume this is a code fragment and that at least one open statement was omitted here, just FYI.

my %genedex;
foreach $line (<INPUT>) {

Don't do that. The foreach reads the whole file into memory and then walks it line by line. If we just want one at a time, let's fetch them that way:


while (defined(my $line = <INPUT>)) {

    my ($gene_name,$remainder) = split(/\t/,$line,2);
    $genedex{$gene_name} = $remainder;
}

foreach $gene (sort keys %genedex) {

You should reopen DATA right here each time through the loop, if you want to start over with it:


open DATA, '<path/to/file.txt' or die "File error: $!";

     while ($line=<DATA>) {
        if ($line =~ /$gene/) {
            ($probe_id) = split(/\s/,$line,2);
            print "$gene\t$probe_id\t$genedex{$gene}\n";
        }
    }

Hope that helps you along.


James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to