There are two ways to accomplish what you want that immediately come to mind: use sprintf() to ensure the proper number of digits and use the magic increment. The first way is simply $newbuildnum = sprintf("%05d", $oldbuildnum+1) Adjust the 5 as needed. %05d is a format specifier that says output an integer that has a width of 5 characters and is 0 padded.

The second method is kind of sneaky. When you have a variable that holds a string, for instance, $a= "foo" and you do $a++, perl will turn that "foo" into "fop". Another $a++ will get you "foq" and so on. This behavior continues wrapping around at "z" (i.e., $a = "foz"; $a++; would result in $a = "fpa") When the string consists of numbers, perl does the same thing only wrapping at "9". Thus:

#!/usr/bin/perl $a = "00001"; for (0..20) { print $a; $a++ } __END__ 00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021

In reply to Re: increment # but keep # of digits same by duff
in thread increment # but keep # of digits same by yankeeblue

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.