cheech has asked for the wisdom of the Perl Monks concerning the following question:
I've done a few programs like this so far and I understand it pretty well. Everything works fine, but again, I feel like there are certain codes that could be done better/more efficiently. Thanks again to those who commented on my post yesterday.. I tried to keep my variables defined locally to the block they are working in. Any comments or suggestions are greatly appreciated. Thanks!
use strict; use warnings; my ($i); #declare global $i for for loop my (@sigmas, @rads); #declare arrays for storage of radii and sigma + results for ($i=1;$i<=100;$i+=2) { # 1, 3, 5, ..., 99 my $r = ($i)*(1e-6); #radii are in microns push (@rads, $r); #store the correct radii in array for pri +nt at end if ($r<2e-5) { #if the radius is <20microns, se +nd to sub sigmalow my $sig = sigmalow($r); #store the return in $sig push (@sigmas, $sig); #add result to the final sigma ar +ray } else { #if the radius is >=20microns, s +end to sub sigmahigh my $sig = sigmahigh($r); #store the return in $sig push (@sigmas, $sig); #add result to the final sigma ar +ray } } print "radii: @rads \n"; print "sigmas: @sigmas \n"; exit; sub sigmalow { #to compute sigma when radius < 20microns my ($pi, $a, $c, $rad, $sig); my (@r); $pi = 3.14159; #constants $a = 1.18; $c = .28e6; @r = @_; #transfer the argument to @r $rad = $r[0]; #is this step necessary? $sig = $pi*($rad**2)*$a*(1-exp(-$c*$rad)); #calculation of sigma return ($sig); #send back the result } sub sigmahigh { #to compute sigma when radius >= 20microns my ($pi, $rad, $sig); my (@r); $pi = 3.14159; #constant @r = @_; #transfer the argument $rad = $r[0]; #necessary? $sig = 2*$pi*($rad**2); #compute return ($sig); #return it }
Sorry about the long, wrapping comments!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Improve my Code: Subroutines
by hangon (Deacon) on Jun 13, 2009 at 21:14 UTC | |
by ikegami (Patriarch) on Jun 13, 2009 at 21:33 UTC | |
by cheech (Beadle) on Jun 14, 2009 at 19:41 UTC | |
by hangon (Deacon) on Jun 15, 2009 at 21:40 UTC | |
|
Re: Improve my Code: Subroutines
by CountZero (Bishop) on Jun 14, 2009 at 08:11 UTC | |
by afoken (Chancellor) on Jun 14, 2009 at 14:55 UTC | |
by CountZero (Bishop) on Jun 14, 2009 at 15:56 UTC | |
|
Re: Improve my Code: Subroutines
by graff (Chancellor) on Jun 14, 2009 at 14:48 UTC | |
|
Re: Improve my Code: Subroutines
by JavaFan (Canon) on Jun 14, 2009 at 14:58 UTC | |
|
Re: Improve my Code: Subroutines
by Marshall (Canon) on Jun 14, 2009 at 16:30 UTC |