Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: New File Not Working

by AnomalousMonk (Bishop)
on Jan 24, 2020 at 19:42 UTC ( #11111840=note: print w/replies, xml ) Need Help??

in reply to New File Not Working

NB: The  \1 \2 \3 notation is for regex back-references and is not kosher in a  s/// replacement string. warnings would have told you this had you enabled them. This (ab)use of back-references is a special dispensation of Perl, but should be avoided in favor of capture variables; see Variables related to regular expressions in perlvar.

c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; ;; my $line = 'OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/ +30/2019,2015-06-20 00:00:00'; ;; $line =~ s/(\d{4})-(\d{2})-(\d{2})\s\d{2}:\d{2}:\d{2}/\2\/\3\/\1/g; print qq{>$line<}; " \2 better written as $2 at -e line 1. \3 better written as $3 at -e line 1. \1 better written as $1 at -e line 1. >OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/2 +0/2015<
Also note that you can avoid inflicting LTS upon yourself (and your eyes) by choosing a better regex delimiter:
c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; ;; my $line = 'OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/ +30/2019,2015-06-20 00:00:00'; ;; $line =~ s{(\d{4})-(\d{2})-(\d{2})\s\d{2}:\d{2}:\d{2}}{$2/$3/$1}g; print qq{>$line<}; " >OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/2 +0/2015<
Please see perlre, perlretut and perlrequick.

Update: Your eyes will also thank you for some whitespace courtesy of the  /x modifier:
    $line =~ s{ (\d{4}) - (\d{2}) - (\d{2}) \s \d{2} : \d{2} : \d{2} }{$2/$3/$1}xg;
And is the  /g modifier really necessary?

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-01-26 11:32 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.