BTW, is your intention to test just the value of "$total_c", or just the value of "$total"? Looking at the code, it seems odd to have a test like $total_c > 750 && $total < 1000 )
Anyway, I'll suggest an alternative that involves less "copy-and-paste" programming. The bad thing about copy-and-paste programming is that when you make one mistake, it turns into lots of mistakes in lots of places. Whenever you find yourself pasting the same code multiple times and changing the same point in each copy, you should be writing a loop or a subroutine instead, so that a given line of code really shows up only once. Oh, and I just noticed that all those copies of those variable names are unnecessary -- even the initial declarations -- because you only use the "$total_c" value from the "default" string, and then assign the original "default" string to some hash element -- you never really seem to do anything useful with all those variables.
So here's something equivalent to what your posted code was trying to do (this alternative has not been tested):
Now, I realize that changing from "%group1, %group2, ..." to %{$group[0]}, %{$group[1]}, ... might cause a major design shift in your code, might cause a lot of rewriting and rethinking, might even seem a bit tedious.my $count = 0; for ( values %default ) # don't really need to know the keys { $count++; my $total_c = substr( $_, rindex( $_, '|' )+1 ); # assume that "group" is an array of hashes: my $groupid = 0; for my $limit ( 750, 1000, 2000, 5000, 10000 ) { if ( $total_c < $limit ) { last; } else { $groupid++; } } $group[$groupid]{$count} = $_; }
But on the whole, the net effect will be to end up with less code -- you'll be deleting relatively lots of unnecessary code, and adding relatively little or none. It'll be time well spent, because it will translate directly into less time spent later, when you need to debug, maintain, update or enhance the code.
(I'm still a bit doubtful about what this code is trying to accomplish, but perhaps that will be for another post...)
In reply to Re: variable issues (they are mean)
by graff
in thread variable issues (they are mean)
by coldfingertips
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |