Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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."

In reply to How to build a better mousetrap (or a variable to hold a column-measuring scale) by roho

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2020-01-20 23:55 GMT
    Find Nodes?
      Voting Booth?