Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^13: appending a variable

by pryrt (Abbot)
on Apr 29, 2022 at 15:46 UTC ( [id://11143441]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143441]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found