in reply to Re: Re: Selecting a random number, and back calculating to chromosome and contig!
in thread Selecting a random number, and back calculating to chromosome and contig!

It looks like you wrote your if/else statements by cutting and pasting each statement, and then modifying each individual statement. While this may feel like real work, it's really not an efficient use of your time. If you ever wanted to modify this program, it would be very difficult to do because of its length. Developing with repetitive code is also prone to typos and other sorts of little errors, as you can see by the conig/contig typos that are in your code. By using a better data structure like thor proposes, you can avoid making minute modifications to repetitive pieces of code (like your if/else statement) and concentrate more on developing your program.

Another idea that you may look into as you learn more is by representing your data and operations on this data in the form of objects. For a reference, check out this short example on OO programming in Perl.

As you develop your code more and more, you may want to begin abstracting your useful code into subroutines. Here's an example that relies on thor's hash-based data structure shown above:

sub print_contig_information { my $number = shift; foreach my $key (sort { $a <=> $b } keys %master) { if ($number < $key) { my $chromosome = $master{$key}{chromosome}; my $contig = $master{$key}{contig}; #do whatever processing here; I'll just print print "chromosome = $chromosome, contig = $contig\n"; } } }
By modularizing your code every way, you avoid having to change the methodology of changing your code in many places every time you'd like to modify your program.

Hope that this helps. :)

  • Comment on Re: Re: Re: Selecting a random number, and back calculating to chromosome and contig!
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Selecting a random number, and back calculating to chromosome and contig!
by Sameet (Beadle) on May 04, 2004 at 05:57 UTC
    HI,
    I have tried doing this, but i keep on getting error messages saying tht the "chromosome" is not numeric to be sorted. It also gives the same error for some contigs. Is there a way around

    Sameet

      You're problem is in the way you initalize %chr_cont_pos. You use parentheses '()' for the inner hashes when you should be using braces '{}'. Refer to perlref for more info.

        hey,

        Thanks, I will rectify this mistake and get back immediately.
        regards
        Sameet