Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing problem

Status
Not open for further replies.

mama12

Programmer
Oct 18, 2005
22
NL
Hallo,

I have a printing error, I can not print 2500 random DNA (A C T G).

can some one here fix my problem

best regards
--------------------------------------------------

#!/usr/bin/perl -w

# 2500 random bases

print "ask for file name: ";


$filename = <STDIN>;

open BN, ">$filename";

my@DNA = ('A','C','G','T');

my $DNA;

for (1 .. 2500) {

$DNA = $DNA[int(rand(length(@DNA)))];
}
print BN "$DNA\n";

close BN;

exit;
 
Hi,mama12.

just some mistake in ur code:

Code:
$filename = <STDIN>;

open BN, ">$filename";
my @DNA = ('A','C','G','T');
my $DNA;

for (1 .. 2500) {

$DNA = $DNA[int(rand([red]scalar[/red] @DNA))];
[blue]print BN "$DNA\n"[/blue];

}

close BN;

U cant use length to get number of elements in an array. (use scalar instead)
 
thanXXXX eewah,

thank u for ur help

 
Normally scalar is redundant - perl is smart enough to work out that if you are using an array in a scalar context, then you must want the number of elements. So for example
Code:
for (my i = 0; i < @array; i++;) {
    # do stuff involving $i here
}
will work fine. It's only when you have a context that accepts a list, like
Code:
print scalar @array;
that you need to coerce it explicitly with scalar.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
also: length() is for strings, not arrays. I am not sure what perl will return if you try that with an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top