Here is an example of a couple of methods of implementing a similar task to what you are trying.

use strict; use warnings; # here is an example that shows how to merge three sub-strings (some o +f which are variables) along with one number that is hardcoded to 2 d +igits my $YYYYmmddHHMMSS = "20220429012345"; for my $num ( 0, 1, 9, 10, 11, 19, 20 ) { my $string = sprintf "%s_%s_%s_%02d\n", "prefix", $YYYYmmddHHMMSS, + "suffix", $num; print $string; } # here is an example that shows how to do a similar merge, but making +the number of digits rely on a variable, rather than being hardcoded +into the format string for my $num_digits ( 1 .. 4 ) { for my $num ( 1, 22, 333 ) { my $string = sprintf "%s_%s_%s_%0*d\n", "new_group", $YYYYmmdd +HHMMSS, "suffix", $num_digits, $num; print $string; } }

This will output

prefix_20220429012345_suffix_00 prefix_20220429012345_suffix_01 prefix_20220429012345_suffix_09 prefix_20220429012345_suffix_10 prefix_20220429012345_suffix_11 prefix_20220429012345_suffix_19 prefix_20220429012345_suffix_20 new_group_20220429012345_suffix_1 new_group_20220429012345_suffix_22 new_group_20220429012345_suffix_333 new_group_20220429012345_suffix_01 new_group_20220429012345_suffix_22 new_group_20220429012345_suffix_333 new_group_20220429012345_suffix_001 new_group_20220429012345_suffix_022 new_group_20220429012345_suffix_333 new_group_20220429012345_suffix_0001 new_group_20220429012345_suffix_0022 new_group_20220429012345_suffix_0333

If you need to, play around with the example, and see if you can figure out a sprintf command that will do 5 digits. Or one that will merge the strings in a different order. Once you've played around some, and understood what this code is doing, then try to apply it to your own project.

update: also, maybe you didn't know, but the page sprintf describes what everything means, including %d vs %2d vs %02d vs %0*d. You should try to read and understand that page. It will really help you.

Generic advice:

If you are unable to learn from this example and apply it to your own project (or, for that matter, from many of the other examples that have been given you throughout this discussion -- I don't claim mine is the ultimate of examples for learning from), then you need to find a "Beginning Perl" course (either video or text-based), and work you way through that, making sure you understand each individual step, until you have learned the variable and string manipulation skills to be able to understand the examples that your fellow Monks have been giving you in this discussion.

You need to get to the point where you can learn from other examples, even if they aren't matching your exact text -- otherwise, you are not writing the code, you are begging others to write snippets for you, then maybe you are trying to glue them together, and hoping it works. You will not be successful in this project or in any future projects if you are unable to apply the examples that you have been given to situations that don't exactly match what you need.


In reply to Re^13: appending a variable by pryrt
in thread appending a variable by pragovnj

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.