Solved

Variable Count with Padding

Posted on 2014-03-25
13
175 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
  • 7
  • 4
  • 2
13 Comments
 
LVL 74

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 74

Expert Comment

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

Author Comment

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

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 74

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now