in reply to Re^9: appending a variable
in thread appending a variable

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^11: appending a variable
by hippo (Archbishop) on Apr 29, 2022 at 14:27 UTC
    But that example will pad an 0 left or right and will make VMs 10-99 as 010 ,, 099

    That is completely incorrect. You haven't even bothered to try it, have you?

    #!/usr/bin/env perl use strict; use warnings; print sprintf "%02d\n", $_ for (0..20);

    🦛

      I am not sure how to use the pad_len, $num in my code. I tried it the below way and it did not work. $OPinst = sprintf("%0*d/PMOSS_SIPNBVQM_5MIN_OHDR_$YYYYmmddHHMMSS$digits");

        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.

Re^11: appending a variable
by Marshall (Canon) on May 01, 2022 at 00:02 UTC
    well, after getting the digits, check if a single digit, if so, add leading zero. Note this also handles the 0->00 case.
    $digits =~ s/^(\d)$/0$1/; # add leading zero if only one digit # 0->00 1->01 2->02 etc.