in reply to Output to table
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Output to table
by Yoda_Oz (Sexton) on Jan 26, 2006 at 02:25 UTC |