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

You're intentionally skipping the first line of your CSV, which is probably because it contains field names, and isn't useful to you. But there's nothing to test your assertion that the first line of the file will be field names, or that it even exists. You might change it to:

my $head = $csv->getline($fh); die "Empty CSV." unless defined $head; warn "Field names detected: (@{$head})\n" if $ENV{MYSCRIPT_DEBUG}; die "No more CSV rows to process after header.\n" if $csv->eof;

Set an environment variable named "MYSCRIPT_DEBUG" to a true value before running if you want debugging info (the warn statement). This will at least test the assertion that your CSV file is not empty, that it starts out with field names, and that there are additional rows available to process after the header row.


Dave

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

    Yes, the first line of the CSV contains field names, sorry for not mentioning that.

    Thank you for the recommendations. I had not tested for the existence of the file or the assertion that the first line is field names because this is a CSV that I made myself to use with this program. It will only be modified when the customer opens new shops. I will follow your recommendations.

    What I'm trying to get to with the program is on one hand to have an array with each product, store & quantity, taken from the EDI file, and on the other to have one array listing store numbers of each type.

    I then plan to go through the "product,store,quantity" array and make a sum of totals per product for each store type (i.e. THSB01 for li type stores = 30, THSB01 for ff type stores = 15, etc.). This is because I need a different price label format for each store type.

    Would it help if I posted dummy store.csv and po.edi files?

    If I put the code for the CSV file by itself and run it, it does what I want it to do (i.e. list of store numbers in two arrays). The problem only happens when I try to integrate that subroutine into the rest of the program.

    Thanks!

      Did you try running it with the additions I proposed? They are tests that attempt to get to the bottom of why your while() loop seems to never be entered. That's relevant information.


      Dave


        I added the code you posted and the program ran the same as before.

        I'm not sure how to set the $ENV{MYSCRIPT_DEBUG} to true. I tried adding the following line before your code, but I guess that's not it?
        my $ENV{MYSCRIPT_DEBUG} = 1;

        Can you tell me how to set the variable to true?