sprintf hack
Often, you need to prefix 0 to maintain uniform column size.
We generally do it like this using sprintf:
for (0..99){
$output = sprintf("%02d", $_);
print $output;
}
A better alternative would be
for (0..99) {
$_ = "0$_" if $_ < 10;
print;
}
Here, $_ is changed only 10 times and we avoid calling sprint 100 times as in earlier case.
We generally do it like this using sprintf:
for (0..99){
$output = sprintf("%02d", $_);
print $output;
}
A better alternative would be
for (0..99) {
$_ = "0$_" if $_ < 10;
print;
}
Here, $_ is changed only 10 times and we avoid calling sprint 100 times as in earlier case.
0 Comments:
Post a Comment
<< Home