[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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
0
stakor
Asked:
stakor
  • 7
  • 4
  • 2
1 Solution
 
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
0
 
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.)
0
 
käµfm³d 👽Commented:
Um, yeah. Isn't that what my example is showing (the console output)?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
stakorAuthor Commented:
So it is. I guess I am just tired. Sorry about that.
0
 
käµfm³d 👽Commented:
Do you understand why it works?
0
 
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.
0
 
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;
}
0
 
TintinCommented:
The usual way is to use printf/sprintf, eg:

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

Open in new window

0
 
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.
0
 
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
0
 
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++;
}
0
 
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

0
 
stakorAuthor Commented:
That works well, thank you.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now