Solved

adding Leading zeros

Posted on 1998-11-12
18
175 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
Comment Utility
printf"%08d",2462;
0
 

Author Comment

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

Expert Comment

by:ozo
Comment Utility
$a = sprintf "%08d", 2462;
0
 

Author Comment

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

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
What's the line in your program before the $a?
0
 

Author Comment

by:Lee5
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Lee5
Comment Utility
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
Comment Utility
How do you conclude that line 2 prints 00002462?
0
 

Author Comment

by:Lee5
Comment Utility
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
Comment Utility
please post test.cgi
0
 

Author Comment

by:Lee5
Comment Utility
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
Comment Utility
$myvar=sprintf("%08d",2345);
0
 

Author Comment

by:Lee5
Comment Utility
why is HaldorG  taking credit for ozo's answer?
0
 

Author Comment

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

Expert Comment

by:ozo
Comment Utility
Sorry, I must have been asleep :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

9 Experts available now in Live!

Get 1:1 Help Now