Variable Count with Padding

I would like to set a variable to 1, and increment it by one, and print it, up to say, 11. But, I would like to make sure that the variable was padded with 4 "0"s (for a total of 5 places). Is there a way to set a numerical variable with padding, and increment it - keeping the padding? I would like to use the variable for naming during file generation.

Desired output would look like:
00001.txt
00002.txt
...
00011.txt
stakorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
If you concatenate the maximum number of zeroes to each number:

e.g.

...
000008
000009
0000010
...

Open in new window


...you can then just take the right side for however many total characters the length should be. You can pass a negative value to substr's second parameter. As a one-liner:

perl -e "foreach (1..10) { print substr(\"00000$_.txt\n\", -10) }"

Open in new window


Screenshot

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stakorAuthor Commented:
This is true, but I was hoping to have 4 zeros before number 1-9 and 3 zeros before the double digit numbers. (2 zeros before the triple digit numbers, etc.)
käµfm³d 👽Commented:
Um, yeah. Isn't that what my example is showing (the console output)?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

stakorAuthor Commented:
So it is. I guess I am just tired. Sorry about that.
käµfm³d 👽Commented:
Do you understand why it works?
stakorAuthor Commented:
Not really.

I am looking to use this for naming files. So I intend to open a directory with a lot of files. I will populate an array with a list of files in the directory, and then I am going to process the files with existing scripts making a call something like system blah.shell <variable_that_looks_like_00001.txt> and then process all the files.

I was looking for a snippet I could fold into the what I was using. I was just planning on posting the almost working script, and ask again, but that will take an hour or two.
stakorAuthor Commented:
my $status = system("prepwork.shell");

@array = <./file.txt>;

foreach $file (@array) {
 my $movestatus = system("mv $file $<variable_that_looks_like_00001>.txt");
 increment $<variable_that_looks_like_00001> somehow;
}
TintinCommented:
The usual way is to use printf/sprintf, eg:

foreach my $i (1 .. 11) {
  printf "%05d\n",$i;
}

Open in new window

stakorAuthor Commented:
I can see a printf would work for printing $i in a specific format. But would you be able to use printf to name a file, that is being called by a system call? Also, I do not know how many files will be in that list. It will be thousands.
käµfm³d 👽Commented:
Well there you go:  Tintin's is a bit more succinct. If you want to "Request Attention" and have the answer switched to his, I won't mind  = )

For completeness regarding my answer:

Let's say that the maximum length of a string you are interested in is 5. As I mentioned above, you would take that many zeroes:

00000

...and prepend it your number value. Start with the simplest case:  an empty string. Prepending an empty string with 5 zeroes you get:

00000

Taking 5 characters from the right, you still have the same 5 zeroes. Now start with 1. Prepend the 5 zeroes:

000001

Yes, you now have 6 characters, but your next step is to take 5 characters from the right side:

00001

So now you have a 1 with 4 leading zeroes. The same holds for all 0-9. Now what happens when you move up to 10:

0000010

Now you have 7 digits. But again, you only take 5, and you take them from the right side:

00010

So now you have 10 with 3 leading zeroes. The same holds true for 10-99. When you move up to 100, the process is the same:

00000100

Take right 5:

00100
stakorAuthor Commented:
So, for this to work in my example, it would be:

my $status = system("prepwork.shell");
my $count = 1;

@array = <./file.txt>;

foreach $file (@array) {
 my $movestatus = system("mv $file $substr(\"00000$count.txt\", -10)");
 $count++;
}
TintinCommented:
You'd do it as:

system("prepwork.shell");
my $count = 1;

foreach my $file (<*.txt>) {
  rename $file,sprintf("%05d.txt",$count) or warn "Could not rename $file $!\n";
  $count++;
}

Open in new window

stakorAuthor Commented:
That works well, thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.