Leading zero in a string

Hi All,

I have a variable I'm reading off of the command line:

my $option = shift;

That variable should hold a number between 1 and 31 (yes, a day of the
month)  I am checking to make sure that the number does indeed lie in
that range.

However, I need to pass that variable to another system command which
expects any "day" value less than 10 to have a leading zero, so 7th
day of the month should say '07'.  How can I check for that leading
zero?  If it's missing, I know I could easily:

$option = "0".$option;

But I can't seem to figure out an easy, clean way to check for it.

--Thanks,

--Errin
0
errinlarsen
9/9/2004 1:58:36 PM
perl.beginners 29388 articles. 4 followers. Follow

5 Replies
895 Views

Similar Articles

[PageSpeed] 31
Get it on Google Play
Get it on Apple App Store

Use the following:

$option = "0" . $option if ($option / 10 < 1 && $option !~ /^0/);

~Sid

On Thu, 9 Sep 2004 08:58:36 -0500, Errin Larsen <errinlarsen@gmail.com> wrote:
> Hi All,
> 
> I have a variable I'm reading off of the command line:
> 
> my $option = shift;
> 
> That variable should hold a number between 1 and 31 (yes, a day of the
> month)  I am checking to make sure that the number does indeed lie in
> that range.
> 
> However, I need to pass that variable to another system command which
> expects any "day" value less than 10 to have a leading zero, so 7th
> day of the month should say '07'.  How can I check for that leading
> zero?  If it's missing, I know I could easily:
> 
> $option = "0".$option;
> 
> But I can't seem to figure out an easy, clean way to check for it.
> 
> --Thanks,
> 
> --Errin
> 
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 



-- 
http://www.upster.blogspot.com
0
siddhu
9/9/2004 2:09:34 PM
Excellent!  Thank you.  I knew it was something easy, just hadn't
kick-started my brain yet this morning.  But I've got another one. 
What if the user input, say, '007' on the command line?  How can I
strip that off?  I think I can check for it with something like this:

/^0?[1-9]/

But If I find it, how do I strip it off?

--Errin


On Thu, 9 Sep 2004 19:39:34 +0530, Sid <siddhu@gmail.com> wrote:
> Use the following:
> 
> $option = "0" . $option if ($option / 10 < 1 && $option !~ /^0/);
> 
> ~Sid
> 
> 
> 
> On Thu, 9 Sep 2004 08:58:36 -0500, Errin Larsen <errinlarsen@gmail.com> wrote:
> > Hi All,
> >
> > I have a variable I'm reading off of the command line:
> >
> > my $option = shift;
> >
> > That variable should hold a number between 1 and 31 (yes, a day of the
> > month)  I am checking to make sure that the number does indeed lie in
> > that range.
> >
> > However, I need to pass that variable to another system command which
> > expects any "day" value less than 10 to have a leading zero, so 7th
> > day of the month should say '07'.  How can I check for that leading
> > zero?  If it's missing, I know I could easily:
> >
> > $option = "0".$option;
> >
> > But I can't seem to figure out an easy, clean way to check for it.
> >
> > --Thanks,
> >
> > --Errin
> > 
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > <http://learn.perl.org/> <http://learn.perl.org/first-response>
> >
> >
> 
> 
> --
> http://www.upster.blogspot.com
>
0
errinlarsen
9/9/2004 2:24:12 PM
Please bottom post....

> Use the following:
> 
> $option = "0" . $option if ($option / 10 < 1 && $option !~ /^0/);
> 

Why divide by 10 first? Why not just check less than 10?  Of course I
would probably drop the maths completely and use C<sprintf>,

perldoc -f sprintf

$option = sprintf('%02d', $option);

Of course I am assuming there has also been,

unless ($option =~ /^\d{2}$/ and $option <= 31) {
   die "I said a date you wacko";
}

And I won't even go into the point that some months only have 28, 29, or
30 days (damn leap years).

http://danconia.org

> ~Sid
> 
> On Thu, 9 Sep 2004 08:58:36 -0500, Errin Larsen
<errinlarsen@gmail.com> wrote:
> > Hi All,
> > 
> > I have a variable I'm reading off of the command line:
> > 
> > my $option = shift;
> > 
> > That variable should hold a number between 1 and 31 (yes, a day of the
> > month)  I am checking to make sure that the number does indeed lie in
> > that range.
> > 
> > However, I need to pass that variable to another system command which
> > expects any "day" value less than 10 to have a leading zero, so 7th
> > day of the month should say '07'.  How can I check for that leading
> > zero?  If it's missing, I know I could easily:
> > 
> > $option = "0".$option;
> > 
> > But I can't seem to figure out an easy, clean way to check for it.
> > 
> > --Thanks,
> > 
> > --Errin
> > 
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > <http://learn.perl.org/> <http://learn.perl.org/first-response>
> > 
> > 
> 
> 
> 
> -- 
> http://www.upster.blogspot.com
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 


0
wiggins
9/9/2004 2:24:34 PM
On Sep 9, Errin Larsen said:

>However, I need to pass that variable to another system command which
>expects any "day" value less than 10 to have a leading zero, so 7th
>day of the month should say '07'.  How can I check for that leading
>zero?  If it's missing, I know I could easily:
>
>$option = "0".$option;

I'd suggest sprintf():

  my $two_digit_day = sprintf "%02d", $one_or_two_digit_day;

To remove them, simply do:

  $date =~ s/^0+//;

That will remove all leading zeroes from a string.

-- 
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart

0
japhy
9/9/2004 2:44:41 PM
--As of Thursday, September 9, 2004 9:24 AM -0500, Errin Larsen is alleged 
to have said:

> Excellent!  Thank you.  I knew it was something easy, just hadn't
> kick-started my brain yet this morning.  But I've got another one.
> What if the user input, say, '007' on the command line?  How can I
> strip that off?  I think I can check for it with something like this:
>
> /^0?[1-9]/
>
> But If I find it, how do I strip it off?

--As for the rest, it is mine.

Don't bother.  ;)

It's a number, after all.  Perl will remove leading zeros for you, as long 
as there are only numbers.  Strip out anything that isn't valid, and add 
the zero when you need it.  (I suggest sprintf, personally.)

Daniel T. Staal

---------------------------------------------------------------
This email copyright the author.  Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes.  This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------
0
DStaal
9/9/2004 9:26:31 PM
Reply:

Similar Artilces:

Zero-width split() match creates empty trailing strings but not empty leading strings
The perlfunc documentation spells this out clearly, and it matches what I see: $ perl -e 'for (split(//, "fob", -1)) { print "$_\n"; }' | sed -e 's/^$/<blank>/' f o b <blank> The question on my mind is why. In particular, is it a decision worth replicating to language's libraries? Thanks for any pointers. I tried to find the source for split, and I think I may have found it in pp_split in pp.c. But there's not really any reason to expect the source code to include the justification, and I couldn't find one. http:/...

How to pad string with leading zeroes
*** please ignore the post entitled 'Brian'. Its a duplicate of *** this post without a matching title Hi, I have a requirement to convert a 3-digit integer to a 3 character string padded with leading zeroes as I SELECT it from the database. Examples: ========= 1 converts to '001' 21 converts to '021' 321 converts to '321' I have played with several string functions to no avail. Any help/suggestions would be appreciated. Brian Two possible ways to get you going: select substring(convert(char(4),column + 1000),2,3) OR ...

Converting numeric value to string with leading zeroes
Hi, We have a smallint column on a table in which we store ISO country numeric codes. These values range in length from 1 to 3 (when stored as smallint). However we now have a requirement that we output them to a flat file they must be of length 3 with leading zeroes if required e.g. 76 must be '076'. Obviously I could just do a few IF statements with DATALENGTH and STR but I was wondering if someone has a nice tidy way if doing it. Eoin > Obviously I could just do a few IF statements with DATALENGTH and STR but I > was wondering if someone has a nice tidy...

Autocomplete dropping leading zeros from results string array
Hi all, I am using the autocomplete control to retrieve values from a DB. When i do a search on a string beginning with a 0 the 0 is dropped from the results shown on screen. I have debugged the GetCompletionList method and the returning string array definetly has the leading zero's. As far as i can tell it only happens with the 0 character.   Any one have any ideas? Cheers,P.S. I am using .net 2.0 with ajax extensions 1.0   Hi WhatStherush, It is a known issue, please add the return items like this, myList.Add("\""+dr.GetString(0)+"\"&quo...

GridView & FormView dropping leading zeros from numbers set as strings
I'm storing in a SQL database a certain field (an employee number) as a varchar data type. Throughout the app (datasource, update/insert stored procedures, etc.) I'm working with it as a string. I've even ensured that it's being used as a string "behind the scenes" by watching it in SQL Profiler. Problem is, the GridView & FormView that display it always drop any leading zeros. Wierder yet, when I put the GridView in Edit mode & add a zero, it saves to the database with the zero, but the leading zero doesn't display in the GridView! Anyone kn...

Autocomplete dropping leading zeros and trailing zeros after a hyphen
Autocomplete Web Service Code 1    Imports System.Web2    Imports System.Web.Services3    Imports System.Web.Services.Protocols4    Imports System.Data.SqlClient5    Imports System.Data6    7    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.8    <System.Web.Script.Services.ScriptService()> _9    <WebService(Namespace:="http://tempuri.org/")> _10   <WebServiceBindi...

Leading zeros
How can you place leading zeros on an integer before converting to string? You can't add leading 0's to the integer but why not do..... MyString = "00000" & str(MyInt) Mat Please try the following varStr = string.Format("{0:000000000}",varNum); ThanksSenthil Nathan http://www.chendhur.com Hi Try as below : Dim PadNbr As String = Trim(Str(12345)) Response.Write(PadNbr.PadLeft(Len(PadNbr) + 1, "0")) HTH K B ShajudeenAreYaar.comDubai, United Arab Emirates....

Leading Zero
Does anyone know of a existing problem from inetcfg bind then reinitialize system then the udp/tcp is putting in a leading zero at the third octect ....192.168.0.21 - 192.168.00.21...This is causing a problem of course....Help Gerald Cummings wrote: > Does anyone know of a existing problem from inetcfg bind then reinitialize > system then the udp/tcp is putting in a leading zero at the third octect > ...192.168.0.21 - 192.168.00.21...This is causing a problem of > course....Help Haven't seen that one... what version of netware, service packs, tcp/ip etc? Is...

Leading zeroes
Hello, How can I suppress the leading zeroes, except one when all positions are 0; for example: 000015 will be 15 000000 will be 0 thank you. Olivier -- +++ GMX - die erste Adresse f�r Mail, Message, More +++ Neu: Preissenkung f�r MMS und FreeMMS! http://www.gmx.net Olivier Wirz wrote: > Hello, > > How can I suppress the leading zeroes, except one when all positions are 0; > for example: > > 000015 will be 15 > 000000 will be 0 This is a pretty vague question. How are you outputting the numbers? Are you writing to a file, using print...

Leading zeros?
I need help with leading zeros within the presentation layer of asp.net. I'm pulling a number from a sql database and display it on a textbox. I want that number to have leading zeros. Example: 149 I want that to be like 00149 or 1234 to be like 01234. I tried several examples provided within some of the previous threads but no luck. Example I use this one: varNum = String.Format("{0:00000}", varStr) Please advise. try something like this : Dim i As Integer, j As String, k As String, val as string i = 123 k = "00000" j = Convert.T...

string() = string()
this code does not work (not instance of object) Dim Items(), Other() As String Items = New String() {"A","B","C"} Other = New String() {} Other = Items how can I get all the values ? angiras Hi, If I understand your requirement correctly, this should do it Dim Items() As String = {"A", "B", "C"} Dim Other(Items.Length - 1) As String Items.CopyTo(Other, 0) To more closely match you code above, but this will be less efficient Dim Items(), Other() As String ...

String and String[]
What will be the advantage of using myString: String[10] over myString: String Man wrote: > What will be the advantage of using > myString: String[10] > over > myString: String "String[10]" is a fixed-length, statically allocated, Ansi-based ShortString. "String" is a dynamic-length, dynamically allocated, reference counted, Ansi-based or Unicode-based (depening on Delphi version) Long string. Unless you absolutely need the static, fixed length nature of ShortString, such as in records that interact with external systems, you should stay ...

string to string[]
Hi, I've got this code : string[] Params; string SQL = "SELECT * FROM T_MANAGEMENT_PAGES"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Params_Type = myReader.GetValue(0).ToString(); } } catch { } finally { } myReader.Close(); My problem is to obtain Params_Type. But each time, it says : "impossible to convert '[object]' in 'string[]' &quo...

Leading zero ?!
Hi,I'm using Convert.ToString() method to convert Hour or Minute of a datetime format control to a string but it's not a leading zero string.  How can I do it ?Please help !cheers,stephen   I think you are using the wrong ToString try DateTime.ToString and System.TimeSpan.  Hope this helps.http://msdn2.microsoft.com/en-us/library/system.timespan.aspxhttp://msdn2.microsoft.com/en-us/library/k494fzbf.aspxKind regards,Gift Peddie  By using the DateTime's ToString and with a format expression, you can format the date into a string...

Web resources about - Leading zero in a string - perl.beginners

Leading - Wikipedia, the free encyclopedia
... here. For the city and metropolitan borough in West Yorkshire, England, see Leeds . For other uses, see Lead (disambiguation) . "Line leading" ...

Buckeye Nation? Ohio State Dominates Oregon in Facebook Conversation Leading Up to Title Game
While the University of Oregon is favored over Ohio State University in Monday night’s College Football Playoff National Championship Game Presented ...

Hürriyet Daily News - LEADING NEWS SOURCE FOR TURKEY AND THE REGION
Hurriyet Daily News is the leading news source for Turkey and the region. Hurriyet daily news, turkish daily news, turkish news, hürriyet daily ...

A World Map of Leading Social Networks
Facebook’s international growth continues at a torrid pace in many countries around the world. Now, Vincenzo Cosenza – translator of Inside Facebook’s ...

Tehran Times - Iran's Leading International Daily
Tehran Times - Iran's Leading International Daily

CNN Leading Women (@CNNIwomen) on Twitter
Log in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here CNN Leading Women @ CNNIwomen ...

Virtual-Strategy Magazine - Your leading source for virtualization news
Virtual Strategy Magazine is an online publication devoted entirely to virtualization technologies.

Daily Times - Leading News Resource of Pakistan
DailyTimes delivers the latest breaking news and information on the latest top stories, entertainment, business, cricket, politics, and more. ...

Mobile News Online - The UK's leading mobile communication industry news site
This month, thousands of students will be starting university for the first time. Some will need new phones. Mystery Caller is on the case to ...

Who are the UK’s Leading Social Business Influencers?
We have attempted to create a list of the top Social Business Influencers in and around the UK. These influencers have, through what they have ...

Resources last updated: 12/1/2015 1:41:07 AM