>>>>> "JF" == Jesus Fernandez <jfer...@tigers.lsu.edu> writes:

  JF> Here is the script:

it helps if your question and code are in the same posting. this will be
a general code review as well as a solution to this last bug.

  JF> jaff
  JF> #!jesusafernandez/bin/perl
  JF> use warnings;

use strict ;

always use both strict and warnings

  JF> for ($samp=0; $samp<5000; $samp++)

for my $samp ( 0 .. 4999 ) {

  JF> {

put your open braces at the end of the line that started the block. you
do it below so be consistant.

  JF> $n = 10000;
  JF> $k = 25;

don't use single letter var names. they convey no meaning and are easily
misunderstood. another reason is that it is hard to search for them!

  JF> while ($k>=2){

you are just counting $k from 25 to 2, so show that in a for
loop. either use the c style loop and count down or a reversed list:

        for my $k ( reverse 2 .. 25 ) {

  JF>     if ($k==1) {last;}

not needed if the loop is coded with for.

  JF>     $mean=(4*$n)/$k*($k-1);

learn about white space. this isn't mathematics, people need to read the
code. also learn about indenting. it would have helped you in solving this.


        my $mean = ( 4 * $n ) / $k * ( $k - 1 ) ;

and if you do know precedence (same for basic math and perl) then the
first set of parens isn't needed.

  JF>     $time=(-log(rand)*$mean);


        my $time = -log(rand) * $mean ;

  JF>     push(@coalt, $time);
  JF>     $k=$k-1;

that is better as $k-- or even $k -= 1 (or $k += -1! :). but as
i said above, a for loop is best
  JF>     }

the close brace is indented ok, the code in the block wasn't.
  JF>     for ($time=0; $time<=24; $time++){
  JF>     }

hmmm. notice there is NO code inside that block. the loop doesn't do anything.

  JF>     print "$coalt[$time]\n";

that print is outside the time loop. if you had used strict you would
have seen that this $time wasn't declared and found the bug. this
statements needs to be inside the previous loop. also indenting would
have helped you see what code is inside which block.
  JF>     }

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to