Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Assigning a parsed date to a variable

by AnomalousMonk (Archbishop)
on Mar 30, 2017 at 19:30 UTC ( [id://1186537]=note: print w/replies, xml ) Need Help??


in reply to Assigning a parsed date to a variable

The  s/// substitution  /r modifier is only available from Perl version 5.14 onwards. kennethk has shown how to use the  /r modifier, but if you have a Perl version prior to 5.14 and wish to preserve the original string, use method B below:

c:\@Work\Perl>perl -wMstrict -le "my $adate = '2017-01-29 11:30:07.370'; ;; my $A_new = $adate =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.*)/ +$2-$3-$1 $4:$5/r; print qq{A: new '$A_new' old '$adate'}; ;; (my $B_new = $adate) =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.* +)/$2-$3-$1 $4:$5/; print qq{B: new '$B_new' old '$adate'}; " A: new '01-29-2017 11:30' old '2017-01-29 11:30:07.370' B: new '01-29-2017 11:30' old '2017-01-29 11:30:07.370'
In both cases, the original string, $adate in this case, is left unchanged. If you do not mind changing this variable, just use a normal  $string =~ s/// substitution.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Assigning a parsed date to a variable
by Anonymous Monk on Mar 30, 2017 at 20:00 UTC
    This line was what I was trying to remember:
    (my $B_new = $adate) =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.*)/$2-$3-$1 $4:$5/;
    Thank you all!
      The difference is context. In scalar context, a regex usually returns the number of matches or the number of substitutions, which is not what you want here. Adding parentheses creates a list context in which a regex will usually return something closer to what you want here.
        Adding parentheses creates a list context ...

        This is either terminal nitpicking on my part or a confession of total ignorance, but I thought that for substitution, the
            ($x = $y) =~ s///;
        form (case B below) worked by circumventing normal operator precedence and making the value, normally a scalar, which is bound by the =~ operator into an assignment expression, which causes the substitution result to be assigned to the assignment lvalue. I.e., list context does not come into play at all. In the
            $x = $y =~ s///;
        statement (case C), the =~ operator has precedence over = and the  s/// return value, the number of substitutions in any context, is assigned. (Somewhere in my Perlish consciousness, I have the idea that substitution normally builds its result in a temporary variable, which is then copied to the bound scalar upon successful completion of the substitution. If an assignment expression is detected, the temp result is simply copied to a different destination.)

        Of course, for a  m// match operation (case A below), list context can be very important.

        c:\@Work\Perl>perl -wMstrict -le "my $y = '2017-01-29 11:30:07.370'; my $x; ;; ($x) = $y =~ m{ (\d{4}) - (\d{2} - \d{2}) \s+ (\d{2} : \d{2}) .* }xms +; print qq{A: x '$x' y '$y'}; ;; ($x = $y) =~ s{ (\d{4}) - (\d{2} - \d{2}) \s+ (\d{2} : \d{2}) .* } {$2-$1 $3}xms; print qq{B: x '$x' y '$y'}; ;; $x = $y =~ s{ (\d{4}) - (\d{2} - \d{2}) \s+ (\d{2} : \d{2}) .* } {$2-$1 $3}xms; print qq{C: x '$x' y '$y'}; " A: x '2017' y '2017-01-29 11:30:07.370' B: x '01-29-2017 11:30' y '2017-01-29 11:30:07.370' C: x '1' y '01-29-2017 11:30'


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1186537]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found