I'm seeking some wisdom after having read Return of Program Repair Shop and Red Flags on perl.com.
The background for this is that I'm storing dates produced by localtime() in an XML file. The application that will be parsing the data (not mine, written in Visual C, can't change it) is expecting dates and times in the format MM-DD-YYYY HH:mm. In order to ensure that dates are formatted as required, I've written a routine to pad numeric values that are not long enough with zeros as follows:
# The appropriate time / date values are retrieved from localtime() ($sec, $min, $hour) = paddatevalues($sec, $min, $hour); sub paddatevalues { foreach my $value (@_) { while (length $value < 2) { $value = '0'.$value; } } return @_; }
The article mentions that whenever you say something like if (length $somenumber < 3), you should think about replacing it with if ($somenumber < 1000). Due to the potential for zero values being passed into my subroutine, a simple if ($value < 9) won't do what I need it to do - I'll end up with a '0' instead of a '00'. Is there a good way to not treat my number as a string without using an if ... elsif to catch the special case (zero)?
--
Grant me the wisdom to shut my mouth when I don't know what I'm talking about.
In reply to Zero-padding integers and the numbers-as-strings Red Flag by Ionizor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |