Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

adding Leading zeros

Posted on 1998-11-12
18
Medium Priority
?
219 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
[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
  • 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
Industry Leaders: 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: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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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

604 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