Solved

adding Leading zeros

Posted on 1998-11-12
18
194 Views
Last Modified: 2010-03-04
In perl, I have a number, say  2462. (it can be any whole number)
I would like to insert zeros at the beginning so that it always becomes a string of 8 characters.
ie  00002462
What is the best way to do this?
0
Comment
Question by:Lee5
  • 9
  • 8
18 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1206153
printf"%08d",2462;
0
 

Author Comment

by:Lee5
ID: 1206154
That prints the number but how do I make it into a variable?
ie:  $a = "00002462"
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206155
$a = sprintf "%08d", 2462;
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:Lee5
ID: 1206156
$a = sprintf "%08d", 2462;
won't compile
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206157
Are you using Perl4?
What does
 perl -v
or
 print $];
tell you?
If you're still using Perl4, you'll have to write that as
 $a = sprintf("%08d", 2462);
(and you'd probably want to upgrade to Perl5)
0
 

Author Comment

by:Lee5
ID: 1206158
Using perl 5.003 under linux
It says I have a syntax error near $a
the prens don't help
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206159
What's the line in your program before the $a?
0
 

Author Comment

by:Lee5
ID: 1206160
Had problem in line before, sorry
Now it adds the 4 zeros, but it also prints them.
If I use a variable instead of the number, it compiles but prints a message saying that I can't use method "a"
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206161
please post the code which adds the 4 zeros, but also prints them,
and the code which prints a message saying that I can't use method "a"
0
 

Author Comment

by:Lee5
ID: 1206162
I got rid of the error but:

$b = "2462";
$a = sprintf"%08d", $b;
print "$a\n";

line 2 prints 00002462
line 3 prints it again
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206163
How do you conclude that line 2 prints 00002462?
0
 

Author Comment

by:Lee5
ID: 1206164
I type   ./test.cgi
If I leave out line 3, I get 00002462
If I add line 3, I get  0000246200002462

I am testing on a unix box, but will be using it on an NT box.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206165
please post test.cgi
0
 

Author Comment

by:Lee5
ID: 1206166
OK I must still be asleep
I did have it in there twice
Thanks so much for your patience

Please make it an answer
0
 
LVL 7

Accepted Solution

by:
HalldorG earned 50 total points
ID: 1206167
$myvar=sprintf("%08d",2345);
0
 

Author Comment

by:Lee5
ID: 1206168
why is HaldorG  taking credit for ozo's answer?
0
 

Author Comment

by:Lee5
ID: 1206169
Sorry, I missed the name on the first box
Thanks to ozo for the help
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206170
Sorry, I must have been asleep :-)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

772 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