http://qs321.pair.com?node_id=1186531


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.