Here is your problem:
push @{hash->{@columnNames}} ,@columnValues;
You are trying to use @columnNames as the key in your hash. Perl automatically takes this in a scalar context, thereby giving you a key of 8 , since there are eight values ββin the array.
What you want to do is treat the first row in CSV (which contains the column names) as special, as these will be the keys to your array.
my @column_names = read_csv_row;
This will give you a hash with a key by column name to array references. Now you need to read in each row of the CSV table and click each field in its correct column hash;
while ( my @employee_fields = read_csv_row ) {
What this does is take each field from the CSV string and paste it into the correct array reference in %employee_hash . I understand that @column_names is in the same order as each row. So $column_names[$field_number] is the correct hash key, and this should match $employee_fields[$field_num] .
However, the structure you said in your post is probably not what you really want. You want something like this:
%VAR = { 7839 => { ENAME => "KING", JOB => "PRESIDENT", MGR => "", HIREDATE => "11/17/1981", SAL => "5000", COMM => "", DEPTNO => "10", } }
This wil key for each employee by their employee number, and all employee fields associated with it will be part of this value. Then you can talk about the job title Employee number 7839 as $employee{7839}->{JOB} , and the employee name is $employee{7839}->{NAME} . Thus, all information about each employee is in one record:
use warnings; use strict; use Data::Dumper; use feature qw(say); my @column_names = read_csv_row();
By the way, I have not tested this code yet. (I will do it now and make the necessary corrections). You might want to play around with Text::CSV , which would be the best way to read in CSV files, and might even help you create these structures (I haven't used it for a long time, so I don't remember everything that it does). However, I believe that you will find that the structure of your employee is a hash of the hashes with the initial hash entered by the employee number, and subahels entered using fields are much better than the hash of arrays.