Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Help with Numbers..Please

by spacey (Scribe)
on Apr 24, 2003 at 09:56 UTC ( [id://252820]=perlquestion: print w/replies, xml ) Need Help??

spacey has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,
Could somebody please suggest a solution to what I hope is a simple problem.

I have some code that increments a number using $number++.
My only problem is that I need the number to increment in full.
What I mean by this is :-
My number is 000001 obviously when I auto increment this using ++ it drops the existing zeros.

Can I overcome this? Or do I have to run a regular
expression on it after incrementing it adding the zeros back in?

Any help on this would be more then appreciated.

Replies are listed 'Best First'.
Re: Help with Numbers..Please
by broquaint (Abbot) on Apr 24, 2003 at 10:05 UTC
    Just make sure your number variable is a string e.g
    my $n = "0001"; print "$n\n" and $n++ for 1 .. 5; __output__ 0001 0002 0003 0004 0005



      The docs say that the "string magic increment" works with variables that

      has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern "/^[a-zA-Z]*[0-9]*\z/"

              dakkar - Mobilis in mobile

      Most of my code is tested...

      Perl is strongly typed, it just has very few types (Dan)

        Thanks for the warning. I have used the magical mystery increment quite often, and didn't realize that it could be problematic depending on previously used context for the variable. I'll have to look over some code to make sure (if I can find it), though I haven't had a problem with it yet.
Re: Help with Numbers..Please
by ph0enix (Friar) on Apr 24, 2003 at 10:04 UTC
    Numerical value of your number is 1 so the 'droping zeros' is correct behaviour. Try printf or sprintf for formating your outputs.
    my $number = 1; printf '%06d', $number;
    or get it back into $number variable

    $number = sprintf('%06d', $number);

Re: Help with Numbers..Please
by Anonymous Monk on Apr 24, 2003 at 10:03 UTC
      Close, but no cigar. This will increment the number after the returned value is used. Either of the next will do, as the modified value isn't being retained:
      $number = sprintf("%06d", ++$number); $number = sprintf("%06d", $number+1);
Re: Help with Numbers..Please
by Improv (Pilgrim) on Apr 24, 2003 at 11:53 UTC
    If you want something for the general case, try the code below..
Re: Help with Numbers..Please
by spacey (Scribe) on Apr 24, 2003 at 10:11 UTC
    Hello Again,

    Thank you All for what must be the fastest reply i have ever had.

    All your information has been most helpful

Re: Help with Numbers..Please
by perlguy (Deacon) on Apr 24, 2003 at 14:45 UTC

    Update: Abigail-II's point is well taken on posting incorrect code. Strike this one and don't bother. I found the problem case after I had already posted. Appologies.

    Update 2: This following regex should work better, and won't fail in the test cases. The same number of digits will be in the output, unless all 9s are in the string, in which case it will just add 1 digit to the front of the string to accomodate. Again, just an exercise.

    $number =~ s/([^9]?9*)$/$1+1/e;

    Back to the original post:

    Here is a regular expression way to do it:

    my $number = '0001999'; $number =~ s/([^9]9+|\d)$/$1+1/e; print "$number\n";

    It has a flaw: if all the numbers are 9, it will drop the last nine and add a 10, so probably not something you would want to use in production code. But the other test cases that I worked with were fine. It was more of an exercise than anything.

      Uhm, if you already know it's flawed, by bother posting? How useful is incorrect code?

      Anyway, here's a regexp solution that is much simpler, and doesn't fail on "9":



        It won't fail on '9', but your proposed alternative regular expression won't work on '009' for example, as '10' would be the output instead of the requested '010'. Just an observation.
Re: Help with Numbers..Please
by Anonymous Monk on Apr 24, 2003 at 14:52 UTC
    Hi Gareth, Use regular numbers to do your calculation and sprintf back into the value that you need. See code below. #!/usr/bin/perl for (0..10) { my $value = sprintf("%.5d",$_); print $value,"\n"; }
Re: Help with Numbers..Please
by dopey (Sexton) on Apr 24, 2003 at 21:38 UTC
    Can't you just auto increment and then add the zeros on the next line?
Re: Help with Numbers..Please
by Anonymous Monk on Apr 25, 2003 at 18:50 UTC
    Hi, If you want the zeros, you should pad these on when they are needed for display. When using the number, it will be simply a number. I have a requirement to do this on my project. I use this function that I wrote:
    sub zeroPad () { my $number = $_[0]; my $charLength = $_[1]; if ($charLength > 10000) { return; } # Sanity Check my $tmp; # Front pad with zeros $tmp = $number; while ( length ($tmp) < $charLength ) { $tmp = "0" . $tmp; } return $tmp; } # end zeroPad

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252820]
Approved by nothingmuch
Front-paged by Trimbach
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-07-20 13:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.