In this problem, I'm looping over radius length from 1 micron to 100 microns in 2 micron intervals. If the length is < 20 microns, I send it to the subroutine "sigmalow," compute sigma, and return it. If the length is >= 20 microns, I send it to a different subroutine because the equation for sigma is different. After all computations, each sigma result is stored in the same array and printed along with the radii.

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!


In reply to Improve my Code: Subroutines by cheech

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.