#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %HoA; while (<>) { chomp; if ( $. <= 12 ) { if (index($_, ',') != -1) { push @{ $HoA{$.} }, split(/,/, ); $. = 0 if $. == 12; # reset line number } else { warn "Line could not be parsed: $_\n"; } } } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl test.csv $VAR1 = { '5' => [ 'Anand', '2', '2', '2', '2', 'tre' ], '1' => [ 'Anand', '1', '2', '3', '4', 'xyz' ], '3' => [ 'Anand', '3', '4', '4', '4', 'ert' ], '2' => [ 'Anand', '2', '3', '4', '5', 'wer' ], '4' => [ 'seetha', '1', '2', '3', '4', 'rew' ] };