in reply to Loading an anonymous hash

What you've got there is an array reference with a hash reference as its only element. To make an anonymous hash reference, you just use curlies ({}). Since you want to read in multiple comma-delimited rows, you probably want an array of hash references, one hashref for each row.

my @records; # store the hashrefs in here my @keys = qw(Emp_No Emp_Lname Emp_Fname Emp_SSN Emp_DOB Emp_aka); open(IN,"input.txt") or die("Can't open input.txt"); while(<IN>) { chomp; my %Employee_Rec; @Employee_Rec{@keys} = split /,/, $_; # hash slice print "$Employee_Rec{Emp_No} \n"; print "$Employee_Rec{Emp_Lname} \n"; print "$Employee_Rec{Emp_Fname} \n"; print "$Employee_Rec{Emp_SSN} \n"; print "$Employee_Rec{Emp_DOB} \n"; print "$Employee_Rec{Emp_aka} \n"; push @records, \%Employee_Rec; } close(IN);

To inspect @records and make sure you've built it correctly, you can use Data::Dumper.

Replies are listed 'Best First'.
Re^2: Loading an anonymous hash
by why_bird (Pilgrim) on Mar 19, 2008 at 15:27 UTC

    Nicely done. I remember trying to do something like this a while back and struggling for a long time because my hash was not properly localised. You have to make sure that my %Employee_Rec is my'd inside that while loop otherwise it will no longer be anonymous and all you'll get is an array full of references to the same hash---the last one you entered...

    Those are my principles. If you don't like them I have others.
    -- Groucho Marx