I'd like to re-arrange a timestamp with a Perl regex with the least code possible. The original format of the time stamp is as follows:
2011/12/29 20:19:45
All I need to do is convert it so that the year at the front instead comes after the month/date as follows:
12/29/2011 20:19:45
I was able to achieve this with the 3 lines of code below. I'm just wondering if there is a way to do this with less code. In particular I tried to do away with the middle line saving $1 into an intermediate variable, and specifying $1 from the first substitution directly in the regex for the second substitution, but this resulted in the error: "Use of uninitialized value $1 in concatenation (.) or string."
If the second line cannot be gotten rid of, then it would seem like this can't be gotten down to one line either?
#my $ts = '2011/12/29 20:19:45'; #input to a subroutine
$ts =~ s/^(\d{4})\///;
my $year = $1;
$ts =~ s/ /\/$year /;