Update: One obvious other thing is: Why push when you can print? Get rid of this push stuff and just output what you want, like this below. You use $r once, calculate some stuff and print it. In the loop below, there is a scaling factor of 1e-6, moving it out of this loop will do pretty much nothing. Below there is a simple integer "if" and a choice of 2 floating point equations. It won't get much better than this.
foreach my $r (@radii) { my $rad = (1e-6)*$r; print "$rad "; if ($r>20) { my $sig = $twopi*($rad*$rad); print "$sig\n"; } else { my $sig = $pi*($rad*$rad)*$a0*(1-exp((-$c0)*$rad)); print "$sig\n"; } }
#3_3.pl #loop over drop radius from 1 micron to 100 microns in steps of 2 micr +ons #use each radius to compute cross section #if radius <20 microns, use (pi)*(r^2)*a*(1-exp(-c*r)) #if radius >= 20 microns, use 2*pi*(r^2) # orginal post at: # http://www.perlmonks.org/?node_id=771126 use strict; use warnings; my @radii = (1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 +, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, + 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99); my @rads = (); my @sigs = (); my $pi = 3.14159; my $twopi = (2)*(3.14159); my $a0 = 1.18; my $c0 = (0.28e6); foreach my $r (@radii) { my $rad = (1e-6)*$r; push (@rads, $rad); if ($r>20) { my $sig = $twopi*($rad*$rad); push (@sigs, $sig); } else { my $sig = $pi*($rad*$rad)*$a0*(1-exp((-$c0)*$rad)); push (@sigs, $sig); } } print "@rads \n"; print "@sigs \n"; __END__ Added some \n for formatting... 1e-006 3e-006 5e-006 7e-006 9e-006 1.1e-005 1.3e-005 1.5e-005 1.7e-005 1.9e-005 2.1e-005 2.3e-005 2.5e-005 2.7e-005 2.9e-005 3.1e-005 3.3e-005 3.5e-005 3.7e-005 3.9e-005 4.1e-005 4.3e-005 4.5e-005 4.7e-005 4.9e-005 5.1e-005 5.3e-005 5.5e-005 5.7e-005 5.9e-005 6.1e-005 6.3e-005 6.5e-005 6.7e-005 6.9e-005 7.1e-005 7.3e-005 7.5e-005 7.7e-005 7.9e-005 8.1e-005 8.3e-005 8.5e-005 8.7e-005 8.9e-005 9.1e-005 9.3e-005 9.5e-005 9.7e-005 9.9e-005 9.05328279702527e-013 1.89602315397586e-011 6.98230615994953e-011 1.56060261711467e-010 2.76113310847353e-010 4.27940909754303e-010 6.10048892522909e-010 8.21584452164295e-010 1.06216829881331e-009 1.33170675846969e-009 2.77088238e-009 3.32380222e-009 3.9269875e-009 4.58043822e-009 5.28415438e-009 6.03813598e-009 6.84238302e-009 7.6968955e-009 8.60167342e-009 9.55671678e-009 1.056202558e-008 1.161759982e-008 1.27234395e-008 1.387954462e-008 1.508591518e-008 1.634255118e-008 1.764945262e-008 1.90066195e-008 2.041405182e-008 2.187174958e-008 2.337971278e-008 2.493794142e-008 2.65464355e-008 2.820519502e-008 2.991421998e-008 3.167351038e-008 3.348306622e-008 3.53428875e-008 3.725297422e-008 3.921332638e-008 4.122394398e-008 4.328482702e-008 4.53959755e-008 4.755738942e-008 4.976906878e-008 5.203101358e-008 5.434322382e-008 5.67056995e-008 5.911844062e-008 6.158144718e-008
In reply to Re: Improve my Code: Subroutines
by Marshall
in thread Improve my Code: Subroutines
by cheech
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |