my $dna=(); foreach my $dna (@random_DNA) { # Line 19 print"$dna\n";} print"\n";# one per line Line 21 my $output="RandDNA .txt"; unless (open(RESULT,">my $output")){ print"Cannot open file\"my $output\".\n\n"; exit; } # Line 26 print RESULT"\n Randomly Generated dna:\n The DNA set containing $number molecules varying in length from $minl + to $maxl bases are:\n\n my $dna\n";

The first line creates the variable $dna with nothing in it (the undef value) and the print at the end trys to print this value resulting in a warning message.    The second line creates a variable named $dna that is local to the foreach loop so the print inside the loop will only produce a warning message if @random_DNA contains an undef value.



my $number= <STDIN>; my $maxl= <STDIN>; my $minl= <STDIN>; # Line 10

You should chomp values you receive via the readline operator.



srand(time|$$);

Unless you have a really really REALLY old version of perl you shouldn't use the srand function.



my $dna; # Line 40 # Set of DNA fragments my @set; # Create set of random DNA: for (my $i=0;$i<$number;++$i) { # find a random length between min & max Line 45: $length= randomlength ($minl,$maxl); # add $dna fragment to @set Line 47: push (@set,$dna);} return @set;} # Line 49

On the first line you create the variable $dna with nothing in it (the undef value) and then push that value into @set many times and then return a list of those undef values.



# Now write the subroutine make_random_DNA: sub make_random_DNA { # Line 57 # Collect arguments, declare variables: my ($length)=@_; # Line 59 my $dna; for (my $i=0;$i<$length;++$i) { $dna.=randomnucleotide();} # Line 62 return $dna;}

This subroutine never gets used anywhere so no random DNA sequences are created.



This may work better, at least it's easier to read (UNTESTED):

#!/usr/bin/perl # Program to generate Random DNA set: use strict; use warnings; print "\n\n Enter No. of DNA Molecules required: "; chomp( my $number = <STDIN> ); print "\n Enter Maximum length of DNA (bases): "; chomp( my $maxl = <STDIN> ); print "\n Enter Minimum length of DNA (bases): "; chomp( my $minl = <STDIN> ); # An array initialized to the empty list, to store the DNA in: # Call the subroutine to do the real job: my @random_DNA = make_random_DNA_set( $minl, $maxl, $number ); # print the results, one per line print "\n The DNA set containing $number DNA molecules, varying in len +gth from $minl to $maxl bases, are:\n\n"; print map( "$_\n", @random_DNA ), "\n"; my $output = 'my RandDNA .txt'; open my $RESULT, '>', $output or die qq[Cannot open file "$output" bec +ause: $!]; print $RESULT <<RESULT; Randomly Generated dna: The DNA set containing $number molecules varying in length from $minl +to $maxl bases are: @random_DNA RESULT exit 0; # Subroutines # Calling the subroutine make_random_DNA_set sub make_random_DNA_set { # Collect arguments, declare variables my ( $minl, $maxl, $number ) = @_; my @set; # Create set of random DNA: for ( 1 .. $number ) { # find a random length between min & max my $length = $minl + int rand $maxl - $minl + 1; # add $dna fragment to @set push @set, make_random_DNA( $length ); } return @set; } # Now write the subroutine make_random_DNA: sub make_random_DNA { my ( $length ) = @_; my @nucleotides = qw( A T G C ); return join '', map $nucleotides[ rand @nucleotides ], 1 .. $l +ength; }

In reply to Re: How can I generate random DNA using the code given by James Tisdall? by jwkrahn
in thread How can I generate random DNA using the code given by James Tisdall? by supriyoch_2008

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.