use strict; use warnings; # here is an example that shows how to merge three sub-strings (some of which are variables) along with one number that is hardcoded to 2 digits 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", $YYYYmmddHHMMSS, "suffix", $num_digits, $num; print $string; } } #### 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