Uri Guttman wrote: >>>>>> "G" == Grant <emailgr...@gmail.com> writes: > > >>> I have a variable which could contain any number from 01-12. I need > >>> to remove the leading zero from numbers 01-09, otherwise I get an > >>> "octal digit" error when the number is used in a calculation. Can > >>> anyone show me how to remove that leading zero? > >> > >> use warnings; > >> use strict; > >> > >> my @numbers = qw ( 01 02 03 04 05 ); > >> > >> for my $num (@numbers) { > >> $num = int $num; > > no need for int. perl coerces numbers to strings and vice versa on > demand. int is actually used to truncate the fraction off of a float, > not to convert a string to a number.
Uri/all, The only reason I knew that int() worked properly in the OP's case is due to a prior (ie not long after my first exposure to Perl) experience where I had to supply a date to a sub in a program I didn't write. This was in the day when each function that dealt with dates would have it's own code to format it. In the particular case I am referring to, the date had to be supplied in dd-mm-yyyy format, but if any one of dd|mm was <=9, only a single digit was acceptable (so I was told). So at that time, I found int() to do the job quite well, and serve as a good reminder when I reviewed the code that I was doing a form of a typecast. I could have also used s/0//, but I knew no better then. In other cases, my predecessor's code would require a '0' be placed before the integer in a date if it was <=9, or it wouldn't work (of course, no errors or warnings, and generally no indication that anything was wrong at all (until someone later asks why some results are "funny")). It was not long after that I started learning about modularity, "require", "use" and code reuse in general ;) Thanks for the tip regarding what int was originally designed for. I didn't know that. Cheers, Steve
smime.p7s
Description: S/MIME Cryptographic Signature