Good, you're learning to "get things done" with Perl. That's one of the first steps; hash out a problem and solve it. That's what Perl is for, and you're on the right track. There is some room for improvement, and if you're open minded you should read on (otherwise, stop reading right now).

Here is one problem: You're calling the ranges 10-20, 20-30, and 30-40. That means by label convention you've got edge cases where the same item could fall into two ranges. For example, 20 fits into the 10-20 range and the 20-30 range. Of course behind the scenes your code is not actually placing the same item into two buckets, but the labels you're using will mislead the user.

Let's assume you want the ranges really to be 10-19, 20-29, and 30-39. And we'll also assume that "range" is an integer.

Your next problem is that you'll soon run out of gas and start asking us about symbolic references if you use variable names like $range1, $range2, and $range3. As soon as you start numbering variable names, you should ask yourself if an array wouldn't be a better solution. (Hint; when you do get to that time in your life where you want to ask about using symbolic references to handle variable variable names, we're full of hasty retorts as to why you shouldn't be asking that question. *grin*)

Next, you're using a C-style 'for' loop just to iterate from '0' to '1'. Break out of that habit. Consider foreach(0..1) ...this is Perl. The C-style loops exist in Perl's syntax, and occasionally are actually useful, but more often than not there's a clearer way to write your code.

Another thing to learn is to adhere to strictures. This means that all of your variables should be declared, and unless there's a good reason, they'll be declared as lexicals using my(). Down the road you'll be glad you learned how to comply with use strict; and use warnings;

I would love to provide an example of how I would re-write this, but you haven't shown us the definition of personCount() and percent(), so I'm not sure what's going on in those little black boxes.


Dave


In reply to Re: Output to table by davido
in thread Output to table by Yoda_Oz

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.