Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Assigning a parsed date to a variable

by kennethk (Abbot)
on Mar 30, 2017 at 18:46 UTC ( #1186531=note: print w/replies, xml ) Need Help??


in reply to Assigning a parsed date to a variable

A substitution regular expression normally changes the string to edit and returns the number of changes. Thus you actually changed your target string for your second case. To get what you meant to get, you would use the code
#!/usr/bin/perl use strict; use warnings; my $adate = "2017-01-29 11:30:07.370"; # more direct way, but returning a "1". my $a_new_datetime = $adate; $a_new_datetime =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.*)/$2-$ +3-$1 $4:$5/; print "\n 1 - $a_new_datetime\n\n"; my $new_datetime = $adate; $new_datetime =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.*)/$2-$3- +$1 $4:$5/; print " 2 - $new_datetime\n\n";
Alternatively, you can use the r modifier (s/_PATTERN_/_REPLACEMENT_/msixpodualngcer in perlop)to shift the behavior to return a value without editing the original string:
#!/usr/bin/perl use strict; use warnings; my $adate = "2017-01-29 11:30:07.370"; # more direct way, but returning a "1". ; printf "\n 1 - %s\n\n", $adate =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):( +\d{2})(.*)/$2-$3-$1 $4:$5/r; printf " 2 - %s\n\n", $adate =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d +{2})(.*)/$2-$3-$1 $4:$5/r;

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-10-19 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (77 votes). Check out past polls.

    Notices?