And now, for a question that will test your tolerance for those with no perl skill whatsoever. I have been trying to write this script that takes data from a file which has two indices indicating a position in the sky, in degrees. It should sum over one of the indices, adding every data point that corresponds to a particular vertical position, and divide by the number of horizontal steps (360).
#!/usr/bin/perl -w use FileHandle; $fh = new FileHandle "/data/jkerwin/muskyoutput/ravsdec.dat"; die "Can't open RAvsDEC file" unless (defined($fh)); @decavg = (); $i=0; $j=0; @line=(); for ($i=0,$i<147,$i++) { $decavg[$i] = 0; } while (defined($strip = <$fh>)) { ($var1) = (split(/\s+/, $strip))[0]; ($var2) = (split(/\s+/, $strip))[1]; ($var3) = (split(/\s+/, $strip))[2]; $line[0] = $var1; $line[1] = $var2; $line[2] = $var3; $decavg[$line[0]] += $line[2]; } for ($i=0,$i<147,$i++) { $decavg[$i] = $decavg[$i]/360; print "$decavg[$i]\n"; }
Trying to get it to print out what is going into the @decavg array gets me only 9 zeros, which is neither what I want, nor the 147 iterations I expect. Every time I have had the program print up until that point I have gotten some kind of output, but there it fails. This next part is where I have failed time and again. I set up a nested for loop that should assign, for all the vertical degrees, the calculated average value to every horizontal step.
@decavg2d = (); for ($i=0,$i<147,$i++) { for ($j=0,$j<360,$j++) { $decavg2d[$i][$j] = 0; } } for ($i=0,$i<147,$i++) { for ($j=0,$j<360,$j++) { $decavg2d[$i][$j] += $decavg[$i]; print "$decavg2d[$i][$j]\n"; } }
Again, when I try to print this out I get a small number of zeroes. Why won't this print out something meaningful? Do I need to use a hash array instead? (I tried, and it gave me the same kind of error). Thanks in advance for any help you can offer me. - jason "newbie" kerwin

In reply to Stupid loops, or is it just me? by jkerwin

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.