Unless you have a compelling reason to have separate arrays, I suggest you use an AoA to put everything in the same data structure. This makes it easier to keep associated values together.

my @radsigma; for ( my $i=1; $i<=100; $i+=2 ) { my $sig; if ( $r <2 e-5 ) { $sig = sigmalow($r); }else{ $sig = sigmahigh($r); } push @radsigma, [ $r, $sig ]; }

The "constant" $pi is used in multiple places and should be declared only once at the top of the program. There is also a pragma to make it more like a real constant.

use strict; use warnings; use constant PI => 3.1416;

You've done way too much work to get the subroutine arguments. This is how it's generally done:

sub sigmalow { my $rad = shift; # shift it off the front of @_ my $rad = $_[0]; # or explicitly assign the value

About your loop, there is no reason to declare $i on a separate line (see above). Also, C style for loops are generally ugly and unperlish. Your example is one of the few cases where these may actually be cleaner than the alternatives, but you should at least be aware that there are alternatives:

for my $i (1..100){ next unless $r % 2; for (0..49){ my $i = $_ * 2 + 1; for my $i ( grep {$_ %2} (0..99 ) ){

A lot of your code is crowded horizontally. Using more blank spaces helps readability.


In reply to Re: Improve my Code: Subroutines by hangon
in thread 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.