#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %HoA;
while (<>) {
chomp;
if ( $. <= 12 ) {
if (index($_, ',') != -1) {
my @fields = split(/,/, );
push @{ $HoA{$fields[0]} }, @fields;
$. = 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 = {
'Anand' => [
'Anand',
'1',
'2',
'3',
'4',
'xyz',
'Anand',
'2',
'3',
'4',
'5',
'wer',
'Anand',
'3',
'4',
'4',
'4',
'ert',
'Anand',
'2',
'2',
'2',
'2',
'tre'
],
'seetha' => [
'seetha',
'1',
'2',
'3',
'4',
'rew'
]
};
####
__DATA__
Anand,1,2,3,4,xyz
Anand,2,3,4,5,wer
Anand,3,4,4,4,ert
seetha,1,2,3,4,rew
Anand,2,2,2,2,tre
####
#!/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'
]
};
####
my %HoA;
while (<>) {
chomp;
if ( $. <= 12 ) {
next unless ( index($_, ',') != -1 ); # or remove it also
push @{ $HoA{$.} }, split(/,/, );
$. = 0 if $. == 12; # reset line number
}
} continue {
close ARGV if eof;
}
####
my %HoA;
while (<>) {
chomp;
next unless ( index($_, ',') != -1 ); # remove if not necessary
push @{ $HoA{$.} }, split(/,/, ) if ( $. <= 12 );
$. = 0 if $. == 12; # reset line number
} continue {
close ARGV if eof;
}
####
my %HoA;
while (<>) {
chomp;
next unless ( index($_, ',') != -1 ); # remove if not necessary
push @{ $HoA{$.} }, split(/,/, ) if ( $. <= 12 );
$. = 0 if $. == 12; # reset line number
} continue {
close ARGV if eof;
}