Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 13th, 2001, 03:47 PM
kumars kumars is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Posts: 9 kumars User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I am new to Perl. Trying to print out a number padded with leading zeros.

example:
$num = 123;

the output/print should have 00123.Total width of 5 padded with leading zeros.
Appreciate any help in this direction.

Thank you


Reply With Quote
  #2  
Old January 13th, 2001, 04:09 PM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 472 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 29 m 6 sec
Reputation Power: 13
Send a message via Skype to mickalo
Thumbs up

Quote:
Originally posted by kumars
I am new to Perl. Trying to print out a number padded with leading zeros.

example:
$num = 123;

the output/print should have 00123.Total width of 5 padded with leading zeros.
Appreciate any help in this direction.

Thank you



Try this:
Code:
my $num = "123"; 
$num = sprintf("%5d", $num);
$num=~ tr/ /0/;


this will "pad" the variable $num with leading "0"'s up to 5 characters: IE 00123 or 01234. If for some reason the $num contains a total of 5 characters, then no "0"'s would be added.

Hope this helps

Mickalo
__________________

Thunder Rain Internet Publishing

Custom Programming & Database development
Providing Personal/Business
Internet Solutions that work!

Reply With Quote
  #3  
Old January 13th, 2001, 05:07 PM
kumars kumars is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Posts: 9 kumars User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Great, it worked.

Thank you Mickalo,

It worked.Thanks again for the fast response.

Reply With Quote
  #4  
Old January 13th, 2001, 05:49 PM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 472 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 29 m 6 sec
Reputation Power: 13
Send a message via Skype to mickalo
Thumbs up

Hey,.. No problems

Glad to help when I can.

Mickalo

Reply With Quote
  #5  
Old January 18th, 2007, 12:54 PM
Correction Correction is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 1 Correction User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 28 sec
Reputation Power: 0
Quote:
Originally Posted by mickalo
Try this:
Code:
my $num = "123"; 
$num = sprintf("%5d", $num);
$num=~ tr/ /0/;


Code:
my $num = "123"; 
$num = sprintf("%05d", $num);

Reply With Quote
  #6  
Old January 19th, 2007, 01:57 AM
PlanB PlanB is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 3 PlanB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m 11 sec
Reputation Power: 0
You can do "substr("00000$num",-5)" as well.
It might be a bit faster though it won't be attractive.

Reply With Quote
  #7  
Old January 19th, 2007, 02:39 AM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 2,687 KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 12 h 58 m 13 sec
Reputation Power: 860
Quote:
Originally Posted by Correction
Code:
my $num = "123"; 
$num = sprintf("%05d", $num);



hehehe... you corrected a 6 year old post! That must be a record.

Reply With Quote
  #8  
Old May 25th, 2010, 09:17 AM
wdrev wdrev is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2010
Posts: 1 wdrev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by KevinADC
hehehe... you corrected a 6 year old post! That must be a record.


It helped me.

Reply With Quote
  #9  
Old January 29th, 2013, 11:40 PM
khirin khirin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 khirin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by wdrev
It helped me.


3 Years later, It helped me too

Reply With Quote
  #10  
Old February 2nd, 2013, 06:50 AM
hofjseph hofjseph is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 hofjseph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 32 sec
Reputation Power: 0
It helped me too

Reply With Quote
  #11  
Old February 4th, 2013, 05:20 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 27 m 4 sec
Reputation Power: 385
Hi,
well I had a very similar problem today at work and I originally used a solution similar to what I thought was the best solution shown so far:

Perl Code:
Original - Perl Code
  1. my $num = "123";
  2. $num = sprintf("%05d", $num);


And it does work as long as you are guaranteed to have digits in your input. Except that when I ran the program, I got an error message because there were a couple of cases where I have letters rather than digits in my input file. And the sprinft formatting string fails in this case. These letters are not really an error, but the indication of a special exception condition that still needs to be handled.

So I think I'll change it to:

Perl Code:
Original - Perl Code
  1. my $num = "123";
  2. $num = sprintf("%05s", $num);


so that if my input is "foo", I'll get "00foo" which, even if it does not seem to make too much sense, is what I need in this case to have the logic of my program working in this case (I need to pad my data with 0's at the beginning of my value, in order to be able to compare data from 2 different files with similar values but different formatting conventions.

This was just to say that the "%05s" format string might just be more general than "%05d" in some real life situations. I haven't tested it against my data yet, I'll do that tomorrow morning.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How to format a string or number with leading zeros?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap