in reply to Re^3: Date/Time Manipulation
in thread Date/Time Manipulation

OK I have been looking at that statement

$stuff =~ s/ (\d\d)/sprintf(" %2.2i",$1-1)/e;

I have poured over my Perl O'Reilly book and am still clueless. Since the input lines look like this

2016-05-16 10:51:07 random text

How does the expression pick out the hours versus any other two digits?

Then what does the sprintf do? I'm willing to learn but would need a starting point.

Replies are listed 'Best First'.
Re^5: Date/Time Manipulation
by haukex (Archbishop) on May 25, 2016 at 18:47 UTC

    Hi RobertJ,

    The regex picks those two digits because they're the only ones preceded by a space.

    As for the rest: because the /e modifier is used on the substitution, the replacement part is evaluated as a Perl expression. sprintf is documented in sprintf: the first argument is the format specifier, in this case %i, aka %d: "a signed integer, in decimal", where 2.2 specifies a minimum width of 2 and a precision of 2 (personally, I would have written it as %02d, TIMTOWTDI). As for the argument to be formatted, $1 refers to the first capture group of the regex, in this case the two digits of the hour, from which 1 hour is subtracted. The formatted string returned by sprintf is then used as the substitution.

    Hope this helps,
    -- Hauke D

      Didn't notice the space (it is an actual character).

      The rest I was able to figure out from O'Reilley and a little Googling.

      Thanks for reminding me that " " is a character.