Solved

Variable Count with Padding

Posted on 2014-03-25
13
179 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 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl count the hash for print 4 163
hard perl script 16 156
Perl script to parse xml file 2 111
Perl string filter 5 78
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

8 Experts available now in Live!

Get 1:1 Help Now