So, apart from figuring out how you really want to spell your variable names (and let me just whisper: "make'em a little shorter"), I think you could simplify the code a lot by using an array of "group" hashes, instead of five completely separate hashes.

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):

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} = $_; }
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.

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

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.