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
HTH
_________ broquaint | [reply] [d/l] |
|
--
dakkar - Mobilis in mobile
Most of my code is tested...
Perl is strongly typed, it just has very few types (Dan) | [reply] [d/l] |
|
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.
| [reply] |
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); | [reply] [d/l] [select] |
Re: Help with Numbers..Please
by Anonymous Monk on Apr 24, 2003 at 10:03 UTC
|
$number = sprintf("%06d", $number++);
| [reply] [d/l] |
|
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);
| [reply] [d/l] |
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..
| [reply] [d/l] |
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
regards
Gareth
| [reply] |
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.
| [reply] [d/l] [select] |
|
s/(\d+)/$1+1/e;
Abigail | [reply] [d/l] |
|
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.
| [reply] |
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";
}
| [reply] |
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? | [reply] |
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
| [reply] [d/l] |