http://www.perlmonks.org?node_id=11105908

roho has asked for the wisdom of the Perl Monks concerning the following question:

I currently set a variable to hold a column-measuring scale from 1 to 220. It is set as a string literal and works fine, but every time I look at it I keep thinking there must be a more compact (and perlish) way to do it. The following code is my first attempt. It loads the value statically to the variable "$old" (for comparison while testing), then loads the value programmatically to the variable "$new". It "works", but there are still lots of numeric literals floating around in there. I say it is now "semi-static" instead of totally static, but I'm sure it can be done better. Can anyone come up with a more generic way of generating this value? TIA.

#!/usr/bin/perl use strict; use warnings; my $old = '|---+----10---+----20---+----30---+----40---+----50---+---- +60---+----70---+----80---+----90---+----100--+----10---+----20---+--- +-30---+----40---+----50---+----60---+----70---+----80---+----90---+-- +--200--+----10---+----20---+----'; my $new = '|---+----'; # Set base value starting with vertical bar. for (my $i = 10; $i <= 220; $i+=10) { my $j = $i; $j = $i-200 if $i > 200 && $i < 300; # Change '210' to '10', '220 +' to '20', etc. $j = $i-100 if $i > 100 && $i < 200; # Change '110' to '10', '120 +' to '20', etc. my $filler = $i == 100 || $i == 200 ? '--+----' : '---+----'; # A +djust filler for 3-digit number. $new .= $j . $filler; } print "\nold=$old\n\nnew=$new\n";

"It's not how hard you work, it's how much you get done."