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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.