Not sure why you're using substr.  Let me suggest that you keep the index and the basename separate.

For example:

my $index = 0; for (my $i = 0; $i < 100; $i++) { $index++; my $fullname = sprintf "base_%04d", $index; printf "fullname = '%s'\n", $fullname; }

will print 100 different names, starting with "name_0001" and ending with "name_0100".  The %04d achieves this result; the "0" left-pads the integer value with zeroes, and the "4" gives you 4 decimal places.

Read up on sprintf to get more details:  perldoc -f sprintf.

If you absolutely must get the last 4 digits of a string, you can use "substr" like this:

$last_4_chars = substr($my_string, -4);

This has the effect of taking the "rest" of the string, starting at the 4th character from the end (since the offset -4 is negative) rather than from the beginning.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re^3: adding 1 to a char of 1 by liverpole
in thread adding 1 to a char of 1 by Anonymous Monk

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.