Hello Ammu007,
Welcome to the Monastery. Your task is very very simple. There are plenty of modules for you to use have you tried any so far? Your problem can be resolved even without any modules simple file parsing.
Try and fail and try again as many times necessary until you succeed. This is the only way for you to learn. Show us where you failed and why you can not proceed and we will be more than happy to assist. Give it a try and update your question.
Update: In high lever description the steps that I would follow, open the file using the eof (*.csv), read line by line the file, split each line so you can retrieve individual elements (such as the name and the string), then simple push the data in a perldsc/HASHES OF ARRAYS and voila you are done. This is the way to proceed without using any module(s).
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my %HoA; while (<>) { chomp; if (index($_, ',') != -1) { my @fields = split(/,/, ); push @{ $HoA{$fields[0]} }, $fields[5]; } else { warn "Line could not be parsed: $_\n"; } } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl test.csv $VAR1 = { 'seetha' => [ 'rew' ], 'Anand' => [ 'xyz', 'wer', 'ert', 'tre' ] };
Update2: In case you want to proceed in using module(s) you can simply use Text::CSV.
#!/usr/bin/env perl use strict; use warnings; use Text::CSV; use Data::Dumper; my $csv = Text::CSV->new({ sep_char => ',' }); my %HoA; while (<>) { chomp; if ($csv->parse($_)) { my @fields = $csv->fields(); push @{ $HoA{$fields[0]} }, $fields[5]; } else { warn "Line could not be parsed: $_\n"; } } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl test.csv $VAR1 = { 'Anand' => [ 'xyz', 'wer', 'ert', 'tre' ], 'seetha' => [ 'rew' ] };
Update3: Sample of *.csv file based on your description.
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
Looking forward to your update, BR.
In reply to Re: Multiple values for a single key (Updated)
by thanos1983
in thread Multiple values for a single key
by Ammu007
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |