On Wed, Jun 27, 2001 at 04:53:04PM -0700, Hans Holtan wrote:
> #!usr/bin/perl  -w
> use strict;

Add:
  use IO::File;

> 
> 
> my ($input,$output,$k,$v,$gene, $input_string, 


Replace this:

> $output1,$output2,$output3,$output4,$output5,

with this:

  @filehandles,


>          $file, $junk,$i,
>          @temp_list,
>          %all_genes);
> 
> 


Replace this:

> #open a new text file
> $output1 =  ">../perl_output/AtChr1.txt";
> open (CHR1, "$output1") or die "Could not open $output1.$!";
> $output2 =  ">../perl_output/AtChr2.txt";
> open (CHR2, "$output2") or die "Could not open $output2.$!";
> $output3 =  ">../perl_output/AtChr3.txt";
> open (CHR3, "$output3") or die "Could not open $output3.$!";
> $output4 =  ">../perl_output/AtChr4.txt";
> open (CHR4, "$output4") or die "Could not open $output4.$!";
> $output5 =  ">../perl_output/AtChr5.txt";
> open (CHR5, "$output5") or die "Could not open $output5.$!";

with this:

    foreach my $n (0 .. 4) {
        my $file = "../perl_output/AtChr$n.txt";

        my $fh = IO::File->new($file, "w") || die("Could not open $file.$!");

        push(@filehandles, $fh);
    }

 
> #open "AtIntergenicTable.txt"
> $input =  "<../perl_output/AtIntergenicTable.txt";
> open (INPUT, "$input") or die "Could not open $input.$!";
> #read it into memory
> while (<INPUT>) {
>          next if (/>/);
>          $input_string .= $_}
> close (INPUT);
> 
> 
> #split it into a hash, geting the TAIR anotation as keys and the next 
> line as the value
> @temp_list = split (/#/, $input_string);
> map (chomp, @temp_list);
> foreach $gene (@temp_list){
>          ($k, $v) = split (/:/, $gene);
>          $all_genes {$k} = $v;
>          }
> 
> 
> #Sort the hash and write to a new file
> foreach $k (sort keys (%all_genes)) {


Replace this:

>          for (1..5){

with this:

           for (0..4) {


>                  if ($k =~ /[$_]g/){


Replace this:

>                          $file = "CHR$_";
>                          print $file ">$k\t$all_genes{$k}\n";
>                          last;

with this:
                           $file = $filehandles[$_];
                           print $file ">$k\t$all_genes{$k}\n";
                           last;

>                  }
>          }
> }


If you see yourself using several similarly named variables with only a
number, in sequence, to distinguish them it's a strong indication you really
want an array.  Also, when you see code that duplicates other code except
for minor differences (as in your open calls) factor out the differences and
make the solution more general.  Finally, if you're trying to get to a bunch
of filehandles, or a bunch of any data type, you should probably consider
putting them in an array or hash, as I've shown.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Reply via email to