Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

New File Not Working

by jalopez453 (Acolyte)
on Jan 24, 2020 at 17:30 UTC ( [id://11111829]=perlquestion: print w/replies, xml ) Need Help??

jalopez453 has asked for the wisdom of the Perl Monks concerning the following question:

I have script to where I am looking to update a date format, but for some reason my new file is not being created and I cannot see what I am missing or what is wrong. If someone could point out what I have wrong it would be truly appreciated.

use strict; open(INFILE, "PSR_Date.txt"); open(OUTFILE, ">PSR_OutputDate.txt"); while (<INFILE>) { $line = $_; $line =~ s/(\d{4})-(\d{2})-(\d{2})\s\d{2}:\d{2}:\d{2}/ +\2\/\3\/\1/g; print OUTFILE "$line"; } close INFILE; print "Done!\n"; chomp($pause = <STDIN>); _DATA_ OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,2015- +06-20 00:00:00 OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,2015- +06-20 00:00:00 OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,2015- +06-20 00:00:00 OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,2015- +06-20 00:00:00 _OUTPUT_ OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/20 +/2015 OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/20 +/2015 OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/20 +/2015 OD44202,07/01/2015,08/19/2019,08/27/2019,1,07/01/2015,06/30/2019,06/20 +/2015

Replies are listed 'Best First'.
Re: New File Not Working
by Corion (Patriarch) on Jan 24, 2020 at 17:33 UTC

    The first step is to ask Perl to tell you about errors:

    open(INFILE, "PSR_Date.txt") or die "Couldn't open input file 'PSR_Date.txt': $!"; open(OUTFILE, ">PSR_OutputDate.txt") or die "Couldn't create output file 'PSR_OutputDate.txt': $!";

      I did do this in regards to verifying the files. I removed the code and everything worked fine. I ended up finding my mistake, I was missing "my" in front of "$line" Thank you for the help.

Re: New File Not Working
by Fletch (Bishop) on Jan 24, 2020 at 17:35 UTC

    ALWAYS CHECK THE RETURN FROM open.

    my $infile = qq{PSR_Date.txt}; my $outfile = qq{PSR_OutputDate.txt}; open( my $infh, q{<}, $infile ) or die qq{Can't open '$infile' to read +: $!\n}; open( my $outfh, q{>}, $outfile ) or die qq{Can't open '$outfile' to w +rite: $!\n}; while( defined( my $line = <$infh> ) ) { ## ... print $outfh $line; } close( $infh ); ## Note that depending on OS, amount written, buffering, phase of moon + you might not see output in the file until you do this. close( $outfh );

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: New File Not Working
by talexb (Chancellor) on Jan 24, 2020 at 17:46 UTC

    Also, it looks like you are looking for dates in YYYY-MM-DD format, yet your data is in MM/DD/YYYY format. You're also going to get just the first date in each line, but I'm guessing that was your intent.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: New File Not Working
by AnomalousMonk (Archbishop) on Jan 24, 2020 at 19:42 UTC

    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:  <%-{-{-{-<

Re: New File Not Working
by GrandFather (Saint) on Jan 25, 2020 at 01:16 UTC

    The code as presented can not have run so the issue you describe is not with that code. Please make sure that the code that you post is the code that you ran. Very often "tidying" the code for publication will turn up the issue if you run the code.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11111829]
Approved by talexb
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-18 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found