Solved

adding Leading zeros

Posted on 1998-11-12
18
187 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 104
perl script help 12 104
perl script to count sepecial characters in a file 7 140
compress files in RAR using perl 13 79
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 …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

910 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

17 Experts available now in Live!

Get 1:1 Help Now