Charles K. Clarkson wrote: > > : While the foreach is certainly more wasteful, why in > : the world would you re-initialize and re-open the > : file multiple times? Why not just open the file once > : and iterate over the file, comparing each line of > : the file to each of the keys in the %input hash? > : > : # My idea > : while (defined(my $line = <DATA>)) { > : foreach my $gene (sort keys %genedex) { > : if ($line =~ /$gene/) { > : ($probe_id) = split(/\s/,$line,2); > : print "$gene\t$probe_id\t$genedex{$gene}\n"; > : } > : } > : } > > I didn't test it, but ... > > my $gene_regex = qr|join '|', keys %genedex|;
THis line won't compile as you have a pipe embedded in a pipe-delimited string. Also, you can't put executable code inside a regex. You need something like: my ($gene_regex) = map qr|$_|, join '|', keys %genedex; > while ( defined( my $line = <DATA> ) ) { > next unless $line =~ /($gene_regex)/; > > my $gene = $1; > > # The split extracts the probe id > printf "%s\t%s\t%s\n", > $gene, ( split /\s/, $line )[0], $genedex{$gene}; > } HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>