Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Multiple values for a single key (Updated)

by thanos1983 (Curate)
on Aug 04, 2017 at 13:12 UTC ( #1196725=note: print w/replies, xml ) Need Help??


in reply to Multiple values for a single key

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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1196725]
help
Chatterbox?
[zentara]: ... an unatural darkness descends on the realm
[shmem]: eclipse? Java? omg...!
[chacham]: The darkness here is quite natural. Sunset was 1.5 hours ago ...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-08-21 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (324 votes). Check out past polls.

    Notices?