in reply to script assistance please

Made the following changes:

  1. Added strict to aid in debugging;
  2. Slight reformat for readability:
  3. #!/usr/bin/perl use strict; $moyr="12/2014"; $,=" "; open(IN,"<COS_SGA_xref.txt"); while(<IN>) { chop; @_ = split(/ /); $csga{$_[0]}=$_[1]; } open(IN,"<cc.txt"); open(OUT,">new_cc.txt"); for($i=0;$i<9;$i++) {$_=<IN>;} ($mo,$yr)=@_ = split(/\//,$moyr); while(<IN>) { chop; @_ = split(/ /); $created=pop(@_); ($mocr,$day,$yrcr)=@_ = split(/\//,$created); if($yrcr.$mocr >= $yr.$mo) { $cc=$_[1]; $cc =~ y/C_//d; splice(@_,3,1); shift(@_); print OUT $created,@_,$csga{$cc}; print OUT "\n"; } } __END__

    Now for the errors:

    D:\PerlMonks>test1.pl Global symbol "$moyr" requires explicit package name at D:\PerlMonks\t +est1.pl line 5. Global symbol "%csga" requires explicit package name at D:\PerlMonks\t +est1.pl line 11. Global symbol "$i" requires explicit package name at D:\PerlMonks\test +1.pl line 15. Global symbol "$i" requires explicit package name at D:\PerlMonks\test +1.pl line 15. Global symbol "$i" requires explicit package name at D:\PerlMonks\test +1.pl line 15. Global symbol "$mo" requires explicit package name at D:\PerlMonks\tes +t1.pl line 16. Global symbol "$yr" requires explicit package name at D:\PerlMonks\tes +t1.pl line 16. Global symbol "$moyr" requires explicit package name at D:\PerlMonks\t +est1.pl line 16. Global symbol "$created" requires explicit package name at D:\PerlMonk +s\test1.pl line 20. Global symbol "$mocr" requires explicit package name at D:\PerlMonks\t +est1.pl line 21. Global symbol "$day" requires explicit package name at D:\PerlMonks\te +st1.pl line 21. Global symbol "$yrcr" requires explicit package name at D:\PerlMonks\t +est1.pl line 21. Global symbol "$created" requires explicit package name at D:\PerlMonk +s\test1.pl line 21. Global symbol "$yrcr" requires explicit package name at D:\PerlMonks\t +est1.pl line 22. Global symbol "$mocr" requires explicit package name at D:\PerlMonks\t +est1.pl line 22. Global symbol "$yr" requires explicit package name at D:\PerlMonks\tes +t1.pl line 22. Global symbol "$mo" requires explicit package name at D:\PerlMonks\tes +t1.pl line 22. Global symbol "$cc" requires explicit package name at D:\PerlMonks\tes +t1.pl line 23. Global symbol "$cc" requires explicit package name at D:\PerlMonks\tes +t1.pl line 24. Global symbol "$created" requires explicit package name at D:\PerlMonk +s\test1.pl line 27. Global symbol "%csga" requires explicit package name at D:\PerlMonks\t +est1.pl line 27. Global symbol "$cc" requires explicit package name at D:\PerlMonks\tes +t1.pl line 27. Execution of D:\PerlMonks\test1.pl aborted due to compilation errors. D:\PerlMonks>

    I will start debugging this, but you should also.
    Holler if the above error messages don't make sense to you.

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

    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.

      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
      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