Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl Monk, Perl Meditation
 
PerlMonks  

Removing leading zeros

by idunno (Beadle)
on May 15, 2001 at 00:31 UTC ( #80333=perlquestion: print w/ replies, xml ) Need Help??
idunno has asked for the wisdom of the Perl Monks concerning the following question:

I have a field with a number that I need to manipulate. The number has an arbitrary number of leading 0's. What's the best way to remove these 0's so I can work with the number?

Thanks

Comment on Removing leading zeros
Re: Removing leading zeros
by FouRPlaY (Monk) on May 15, 2001 at 00:34 UTC
    I'd do it with a regex like:

    s/^0+//g



    FouRPlaY
    Learning Perl or Going To die() Trying
(Ovid) Re: Removing leading zeros
by Ovid (Cardinal) on May 15, 2001 at 00:34 UTC
Re: Removing leading zeros
by suaveant (Parson) on May 15, 2001 at 00:36 UTC
    Just use it as a number, like $var + 100 or $var == 6 as long as you do that, perl will treat it as a number and ignore leading zero's, if you do $var eq 6 it will not remove zeros, because eq is for strings... to just convert it directly to a number for peace of mind you can do $var += 0 which will turn 00005.67 into 5.67 in $var.
                    - Ant
Re: Removing leading zeros
by Banky (Acolyte) on May 15, 2001 at 00:37 UTC
    If you need to worry about possible negatives.
    $num=~s/^(-?)0*/$1/;
Re: Removing leading zeros
by physi (Friar) on May 15, 2001 at 00:53 UTC
    Sorry brother's,
    but the following doesn't work like idunno needs it:
    perl -e ' $e = 0034; $e+=0;print $e'
    Gives us 28 instead of 34 !!!

    This only work if we change this to:

    perl -e ' $e = "0034"; $e+=0;print $e'
    What happens is that the number 0034 is internaly used by perl as an octal number. And octal 034 is Dec: 28 !!!
    what can we do ???

    Any hints ?

    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
      Octal interpretation only happens for constants. Removing leading zeroes from a constant is trivial; you just use the Delete key on your keyboard.

      Removing leading zeroes from a number held in a variable (presumably what the original poster was asking) is a different question; various solutions have already been provided. In this case, however, suaveant is correct that you don't need to remove the leading zeroes. If you treat the value as a number, Perl is happy to oblige!

Re: Removing leading zeros
by wardk (Deacon) on May 15, 2001 at 02:31 UTC

    didn't see a sprintf solution...

    #!/usr/bin/perl use strict; my @num = ( "000006", "00005.55", "0004.5650"); foreach (@num) { print $_, " becomes: "; print sprintf("%d\n", $_); }
    which produces....
    000006 becomes: 6 00005.55 becomes: 5 0004.5650 becomes: 4
Re: Removing leading zeros
by cLive ;-) (Parson) on May 15, 2001 at 03:38 UTC

    Argh, everybody's gone regex mad!

    1. if you're using the number mathematically, you don't need to remove the leading zeroes; or
    2. $var+=0;
    cLive ;-)
Benchmarking Removing leading zeros
by grinder (Bishop) on May 15, 2001 at 12:48 UTC
    Looks like += 0 is the winner, sprintf is the loser and regex comes somewhere in the middle.
    #! /usr/bin/perl -w use strict; use Benchmark; my $start = shift; timethese shift, { Add => sub { my $num = $start; $num += 0 }, Regex => sub { my $num = $start; $num =~ s/^0+// }, Sprintf => sub { my $num = $start; $num = sprintf '%d', $num }, };
    % perl zero 0000000000000000000000123 2000000
    Benchmark: timing 2000000 iterations of Add, Regex, Sprintf...
           Add: 10 wallclock secs ( 9.30 usr +  0.00 sys =  9.30 CPU)
         Regex: 10 wallclock secs (10.30 usr +  0.00 sys = 10.30 CPU)
       Sprintf: 15 wallclock secs (14.39 usr +  0.02 sys = 14.41 CPU)
    % perl zero 0123 2000000
    Benchmark: timing 2000000 iterations of Add, Regex, Sprintf...
           Add:  8 wallclock secs ( 8.77 usr +  0.00 sys =  8.77 CPU)
         Regex: 11 wallclock secs ( 9.88 usr +  0.00 sys =  9.88 CPU)
       Sprintf: 13 wallclock secs (11.95 usr +  0.00 sys = 11.95 CPU)
    
    

    --
    g r i n d e r

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://80333]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-04-21 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls