in reply to Formatting date strings (was: date +%Y%m%d)
There are a few ways to do this in perl. It's a good day to learn about array slices and s/printf.
# this takes the 4th through sixth elements of the array # returned by localtime and sets @times to them my @times = (localtime)[3..5]; my $date = sprintf("%4d%02d%02d", $times[2]+1900, $times[1]+1, $times[ +0]);
The "%02d" formatting string (roughly: give me two digits, add enough '0's to the front to make the field two wide) is highly useful for 0-padding things to a specified length (in this case, 2).
Overall, it's more typing than the external call to date? Sure, but it's more portable and (although I haven't benchmarked it), more efficient (doesn't spawn a subshell).
HTH.
perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
|
|---|