in reply to Re: script assistance please
in thread script assistance please

Made the following changes:

  1. Closed your files when you were done with them.;
  2. Declared your variables (required under strict and intensely recommended for a huge list of reasons)
  3. #!/usr/bin/perl use strict; my %csga = (); my $moyr="12/2014"; $,="\t"; open(IN,"<COS_SGA_xref.txt"); while(<IN>) { chop; @_ = split(/\t/); $csga{$_[0]}=$_[1]; } close IN; open(IN,"<cc.txt"); open(OUT,">new_cc.txt"); for(my $i=0;$i<9;$i++) {$_=<IN>;} my ($mo,$yr)=@_ = split(/\//,$moyr); while(<IN>) { chop; @_ = split(/\t/); my $created=pop(@_); my ($mocr,$day,$yrcr)=@_ = split(/\//,$created); if($yrcr.$mocr >= $yr.$mo) { my $cc=$_[1]; $cc =~ y/C_//d; splice(@_,3,1); shift(@_); print OUT $created,@_,$csga{$cc}; print OUT "\n"; } } close IN; close OUT; __END__

    And the results:

    D:\PerlMonks>test2.pl D:\PerlMonks>

    It created an empty output file called new_cc.txt. I presume that's because I did not have the input file COS_SGA_xref.txt.

    I'm afraid additional debugging will be all guesswork unless you can provide sample input data.

    At a guess, however, your problem is one of two things:

    1. Since you didn't close IN, your attempt to open it again failed.
      • If you would check for errors on open, you could catch such things.
    2. You only write output if this statement is true: if($yrcr.$mocr >= $yr.$mo). Maybe it never is?
      • A print statement right before the if could reveal the issue.

Replies are listed 'Best First'.
Re^3: script assistance please
by marinersk (Priest) on Feb 19, 2015 at 22:09 UTC

    Made the following changes:

    1. Checked status of open statements and displayed errors where appropriate;
    2. Displayed suggested debugging statements.
      • NOTE: These might mess with your use of $_ and @_. I haven't used those in so long I've forgotten the rules.
    3. #!/usr/bin/perl use strict; my %csga = (); my $moyr="12/2014"; $,="\t"; if (!open(IN,"<COS_SGA_xref.txt")) { print "ERROR: Cannot open input file COS_SGA_xref.txt\n"; } else { while(<IN>) { # Using chop instead of chomp here makes me nervous - marine +rsk chop; @_ = split(/\t/); $csga{$_[0]}=$_[1]; } close IN; if (!open(IN,"<cc.txt")) { print "ERROR: Cannot open input file cc.txt\n"; } else { if (!open(OUT,">new_cc.txt")) { print "ERROR: Cannot open output file new_cc.txt\n"; } else { for(my $i=0;$i<9;$i++) {$_=<IN>;} my ($mo,$yr)=@_ = split(/\//,$moyr); while(<IN>) { # Use of chop instead of chomp here makes me nervo +us - marinersk chop; @_ = split(/\t/); my $created=pop(@_); my ($mocr,$day,$yrcr)=@_ = split(/\//,$created); print "DEBUG: -----------------------------\n"; print "DEBUG: \$yrcr.\$mocr = [$yrcr.$mocr]\n"; print "DEBUG: \$yr.\$mo = [$yr.$mo]\n"; if($yrcr.$mocr >= $yr.$mo) { my $cc=$_[1]; $cc =~ y/C_//d; splice(@_,3,1); shift(@_); print OUT $created,@_,$csga{$cc}; print OUT "\n"; } } # Cleanup close OUT; } # Cleanup close IN; } } __END__

      And the results:

      D:\PerlMonks>test3.pl ERROR: Cannot open input file COS_SGA_xref.txt D:\PerlMonks>

      Any further debugging requires information from you.

      Good luck!

      COS_SGA_xref.txt 195003 COS 195010 COS 500512 COS 500511 COS 520536 COS 550551 COS 550625 COS cc.txt 01/31/2015 Dynamic List Display + 1 Controlling Area C2 Date 01/01/1900 To 12/31/9999 Cost Center All Cost Centers Cost Ctr Description CoCd Profit Ctr Person Respon +sible Created on 6 PCS<>PCS MAIL CLOSED per B. Middleton 301 6000 G +arrity, T. 05/24/2004 30 COST CENTER 000001 170 112 Mac Crawford 08/3 +0/2004 50 150BS - Balance Sheet C/C-(No RT) 150 112 Mac C +rawford 08/16/2004 60 COST CENTER 000001 160 112 Mac Crawford 08/1 +6/2004 60 COST CENTER 000001 170 112 Mac Crawford 08/1 +6/2004 70 COST CENTER 000001 170 112 Mac Crawford 08/1 +6/2004 71 COST CENTER 000001 170 112 Mac Crawford 08/1 +6/2004 74 COST CENTER 000001 170 112 Mac Crawford 08/1 +6/2004 76 COST CENTER 000001 170 112 Mac Crawford 08/3 +0/2004 77 COST CENTER 000001 170 112 Mac Crawford 08/3 +0/2004 78 COST CENTER 000001 170 112 Mac Crawford 08/3 +0/2004 80 State of NY 351 112 Bob Achettu 02/09/2006

        Using this data against my test3.pl, I get the following results:

        D:\PerlMonks>test3.pl DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2004.08] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [2006.02] DEBUG: $yr.$mo = [2014.12] DEBUG: ----------------------------- DEBUG: $yrcr.$mocr = [.] DEBUG: $yr.$mo = [2014.12] D:\PerlMonks>

        I still submit that your failure to close IN before re-opening it is probably still part of your problem.

        Regardless, as you can see from the debugging statements, your if() statement is never true, so the script never has any reason to write any data.

        Good luck!

Re^3: script assistance please
by perlnobie (Initiate) on Feb 19, 2015 at 22:12 UTC
    Marinersk, thanks for your reply. I hope this works out. Sample data COS_SGA_xref.txt 195003 COS 195010 COS 500512 COS 500511 COS 520536 COS 550551 COS 550625 COS 560119 COS cc.txt 01/31/2015 Dynamic List Display 1 Controlling Area C2 Date 01/01/1900 To 12/31/9999 Cost Center All Cost Centers Cost Ctr Description CoCd Profit Ctr Person Responsible Created on 6 PCS<>PCS MAIL CLOSED per B. Middleton 301 6000 Garrity, T. 05/24/2004 30 COST CENTER 000001 170 112 Mac Crawford 08/30/2004 50 150BS - Balance Sheet C/C-(No RT) 150 112 Mac Crawford 08/16/2004 60 COST CENTER 000001 160 112 Mac Crawford 08/16/2004 60 COST CENTER 000001 170 112 Mac Crawford 08/16/2004 70 COST CENTER 000001 170 112 Mac Crawford 08/16/2004 71 COST CENTER 000001 170 112 Mac Crawford 08/16/2004 74 COST CENTER 000001 170 112 Mac Crawford 08/16/2004 76 COST CENTER 000001 170 112 Mac Crawford 08/30/2004 77 COST CENTER 000001 170 112 Mac Crawford 08/30/2004 78 COST CENTER 000001 170 112 Mac Crawford 08/30/2004 80 State of NY 351 112 Bob Achettu 02/09/2006 80 State of NY 301 112 Bob Achettu 02/09/2006