in reply to Cannot work on second file after reading first file.

Hi,

just a guess. I've to cite the man page for Text::CSV: "...getline...It reads a row from the IO object $io using $io->getline () and parses this row into an array ref. This array ref is returned by the function or undef for failure.".

IMHO you're not checking the case of a failure, so it seems possible that the while loop is exited because of an error.

UPDATE: There is another problem:

my (edi_file, $po_data, @po_array);

I get a compiling error thrown.

Regards
McA

Replies are listed 'Best First'.
Re^2: Cannot work on second file after reading first file.
by davido (Cardinal) on Feb 28, 2014 at 16:15 UTC

    Presumably Text::CSV's auto_diag attribute works correctly, so testing for error isn't so much the issue as testing for additional lines to get.


    Dave

      You're right. I saw it afterwards that the auto_diag flag is set. So, I'm happy I wrote "guess" ;-)

      Regards
      McA

        Good catch on the compilation error. That tells me one thing; we're not looking at actual code capable of compiling and reproducing the problem, which means we're wasting our time. The actual code could have some problem that hasn't been demonstrated.


        Dave

Re^2: Cannot work on second file after reading first file.
by 1straw (Novice) on Feb 28, 2014 at 16:52 UTC

    Thank you McA. Sorry for the error. I was writing the program with variables in Spanish and decided to change to English before posting here. I ran a search & replace on that variable and replaced without $. All edi_file should be $edi_file.

      I've added some comments to your separate_stores function to try to better explain the potential issues I see.

      sub separate_stores { my (@li_stores, @ff_stores); my $stores_file = File::HomeDir->my_home . "/example/data/example.cs +v"; open my $fh, "<", $stores_file or die "$stores_file: $!"; # So at this point we know that you were able to successfully # open example.csv, because you tested your open. my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, }); # Also know that if your parsing of CSV generates an error, # that error will be spat to the screen for us. Good. my $count_li = 0; my $count_ff = 0; $csv ->getline ($fh); # We don't know if the preceding line returned 'undef', # indicating an empty CSV file. We also don't know if # it really stripped away a header row, or something useful # instead. We also don't know if there is any more CSV remaining # after processing that first line. ################# # Change the preceding line "$csv ->getline ($fh);" to the following, # for better diagnostics: # my $head = $csv->getline($fh) or die "Empty CSV file $!\n"; warn "CSV header line contained (@{$head})\n" if $ENV{MYTEST_DEBUG}; die "Nothing found after CSV header line.\n" if $csv->eof; # # Now set an environment variable "MYTEST_DEBUG" true, and re-run your + test. ################# while (my $row = $csv->getline ($fh)) { # Your 'while' loop will never be entered if you're already at # the end of file. We don't know if that's an issue, because you # didn't explicitly test what happened when you stripped the first # CSV line. if ($row->[1] eq "li") { $li_stores[$count_li] = $row->[0]; $count_li ++; } else { $ff_stores[$count_ff] = $row->[0]; $count_ff ++; } } close $fh; return (\@li_stores, \@ff_stores); }

      It's almost certain that if this subroutine is failing in the big script, one of those assertions will also fail in the big script. And that will give you better information on why your subroutine is failing to do what you want.


      Dave


        Thank you for the explanation. I set MYSCRIPT_DEBUG to true. When I ran the script it printed:
        Field names detected: (# de tienda tipo ubicacion

        with the exception of "(", these are the names of the fields

        Sorry for the ignorance, how do I set the environment variable to true?

        Hi Dave,

        I apologize, I don't mean to waste your time.

        The code I posted is all the code, there's nothing else.

        I added the warn inside the while loop and it does not print. I had done something similar, using print instead of warn, which is how I (thought I) knew it was never entering the loop

        As I said, it works perfectly when run independently of the large script. It stops working when I add it to it

        Thanks

        Yes, both scripts are running as the same user

        I have updated my initial question with a sample CSV and a sample EDI file