Greetings, Monks!

I am a relatively new Perl programmer. One of my variables is becoming undefined, and I do not know why.

I wanted a script to read in a series of csv files with column headers, and create a main hash, $file_data, to store an array of records for each file. Each 'record' is a hash reference. Everything was working yesterday, but I must have done something, because now the main hash becomes empty after the loop that reads the files.

Here is the code:

@rem = 'source http://www.dostips.com @CD/d"%~dp0"&perl -s "%~nx0" %*&Exit/b&:'; #perl script starts below here use Warnings; use Strict; use File::Basename; my %file_data; my @field_headers = (); open(OUT, ">inbound.txt") || die("can't open output file: $!"); # read all of the files dropped onto the batch file while (<>) { #grab and save the column headers on the first line if($. == 1) { @field_headers = @{split_csv_string($_)} ; # read the rest of the lines. each 'record' is a hash with the # field name as the key. each 'record' is added to the array of # records for that file } else { my %record; my $i = 0; foreach my $field_val ( @{split_csv_string($_)} ) { $record{ $field_headers[$i] } = $field_val; $i++; } # ****** $file_data IS OK HERE! ****** push @{ $file_data{ $ARGV } }, \%record; } # reset line numbering on each input file if (eof) { # Not eof(). close(ARGV); @field_headers = (); } } # ****** $file_data IS BLANK HERE! ******

In reply to Why Does My Hash Become Undefined? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.