[ic] {Spam?} Leading 0 - calc issue.

David Christensen david at endpoint.com
Tue Sep 6 19:41:41 UTC 2011


On Sep 6, 2011, at 2:27 PM, Rick Bragg wrote:

> I am trying to increment a number and keep it's leading 0.  (I need the
> result to be 2 digits long)  
> 
> The following works fine and returns "08":
> 
> [tmp number]07[/tmp]
> [calc]
> my $num = [scratch number]+1;
> $num = sprintf("%2d", $num);
> $num =~ tr/ /0/;
> return $num;
> [/calc]
> 
> However, if I use 08, or 09, I get "0" returned.  Any idea why?

Perl parses a number with a leading zero as indicating an octal digit, so I suspect it's interpreting [scratch number] as an octal number due to the leading zero when it's evaluating the [calc] block.  If you don't need the leading zero in the initial number, you can simply use sprint's built-in zero-pad option to zero pad to  the target length anyway and just avoid the issue all-together.

e.g., sprintf('%02d', $num);

> [tmp number]08[/tmp]
> [calc]
> my $num = [scratch number]+1;
> $num = sprintf("%2d", $num);
> $num =~ tr/ /0/;
> return $num;
> [/calc]
> 
> [tmp number]09[/tmp]
> [calc]
> my $num = [scratch number]+1;
> $num = sprintf("%2d", $num);
> $num =~ tr/ /0/;
> return $num;
> [/calc]
> 
> Thanks!
> Rick
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> interchange-users mailing list
> interchange-users at icdevgroup.org
> http://www.icdevgroup.org/mailman/listinfo/interchange-users

Regards,

David
--
David Christensen
End Point Corporation
david at endpoint.com







More information about the interchange-users mailing list