Solved

Variable Count with Padding

Posted on 2014-03-25
13
182 Views
Last Modified: 2014-03-25
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
Comment
Question by:stakor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39953022
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
 

Author Comment

by:stakor
ID: 39953068
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39953086
Um, yeah. Isn't that what my example is showing (the console output)?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stakor
ID: 39953093
So it is. I guess I am just tired. Sorry about that.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39953097
Do you understand why it works?
0
 

Author Comment

by:stakor
ID: 39954707
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
 

Author Comment

by:stakor
ID: 39954784
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
 
LVL 48

Expert Comment

by:Tintin
ID: 39954833
The usual way is to use printf/sprintf, eg:

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

Open in new window

0
 

Author Comment

by:stakor
ID: 39954837
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39954840
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
 

Author Comment

by:stakor
ID: 39954927
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
 
LVL 48

Expert Comment

by:Tintin
ID: 39955037
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
 

Author Comment

by:stakor
ID: 39955177
That works well, thank you.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question