in reply to Why Does My Hash Become Undefined?

I rewrote a bit to get to work on command line with manually entered input:
use warnings; use strict; use File::Basename; use Data::Dumper; 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 (<>) { if(!m/^quit$/) { #grab and save the column headers on the first line if($. == 1) { #@field_headers = @{split_csv_string($_)} ; @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 } else { close(ARGV); @field_headers = (); } } sub split_csv_string { my ($str) = @_; chop($str); return split q|,|,$str; } print Dumper %file_data;
When I manually enter a,b,c ; 1,2,3 ; 4,5,6 (where ';' is actually an CRLF) I get:
$VAR1 = '-'; $VAR2 = [ { 'c' => '3', 'a' => '1', 'b' => '2' }, { 'c' => '6', 'a' => '4', 'b' => '5' } ];
I realize this output would probably look different run in your environment and the way you are running your code. You can open a filehandle for diagnostic output, run it in your environment, and see what you get and operate from there. I would personally recommend that you rewrite this code entirely and use Text::CSV in a way that you can easily debug when needed.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: Why Does My Hash Become Undefined?
by Anonymous Monk on Apr 08, 2011 at 13:04 UTC

    Thank You! That Worked!!

    I agree that Text::CSV would have been better, but I am in a corporate environment where I cannot use anything that was not installed with ActiveState. I find myself reinventing the wheel over and over again.