On Sun, 24 Apr 2011 18:14:52 -0400, galeb abu-ali wrote: > I revised the code to the following: [...] > for my $file( @ARGV ) { > open( my $IN, "<", $file ) or die "Failed to open: $!\n"; > > my( %cog_cat, %cog_id, @cogs, $oid, $locus, $source, $cluster_info > ); > > while( my $line = <$IN> ) { > chomp $line; > > if( $line=~ /COG_category/ ) { > ( $oid, $locus, $source, $cluster_info ) = split /\t/, > $line; > > push @{ $cog_cat{ $locus } }, $cluster_info if( > $cluster_info ); > > } elsif ( $line=~ /COG\d+/ ) { > ( $oid, $locus, $source, $cluster_info ) = split /\t/, > $line; push @{ $cog_id{ $locus } }, $source if( $source ); > push @{ $cog_id{ $locus } }, $cluster_info if( $cluster_info > ); > } > }
Be ruthless about removing duplication. The more unnecessary code you can prune, the more what's left reveals its true intention, like chiseling away everything that is not David from a block of marble :-) So in the above, you can take the line ( $oid, $locus, $source, $cluster_info ) = split /\t/, $line; from both clauses and put it before the if statement. Also, Perl's topic variable allows you to eliminate the use of certain variables that otherwise serve no purpose, like $line in your code is only there to get at its contents. So instead you can say while (<$IN>) { chomp; if ( /COG_CATEGORY/ ) { my ( $oid, $locus, $source, $cluster_info ) = split /\t/; See also how I declared those four variables right there? Remove their declaration from before the while loop now and you've gotten rid of some more duplication and unnecessarily wide scoping. > Many thanks again! Must've spent ~ 4 days on this. I've been flirting > with Perl less than a year, it's so seductive I find myself debating > whether to go back to school. Heh, camels can be like that :-) -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/ http://www.informit.com/store/product.aspx?isbn=0137001274 http://www.oreillyschool.com/courses/perl3/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/