Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Multiple values for a single key

by Ammu007 (Initiate)
on Aug 04, 2017 at 13:06 UTC ( [id://1196722]=perlquestion: print w/replies, xml ) Need Help??

Ammu007 has asked for the wisdom of the Perl Monks concerning the following question:

Hi i have a .csv file where i have the following values 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 i need to parse through these rows and for single value of Anand i need to map the last coloumn values like Anand xyz wer ert tre can i use hashmap to store this value,if so how?single key and different values....please help

Replies are listed 'Best First'.
Re: Multiple values for a single key
by talexb (Chancellor) on Aug 04, 2017 at 13:59 UTC

    The general solution to your question is to store multiple values in an array that share a single key.

    So instead of having Anand => 'xyz', you would have Anand => [ 'xyz', 'wer', 'ert', 'rew', 'tre' ]. Write some code and see how far you get, and we'll be glad to help you if you run into problems. We won't write the code for you.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Multiple values for a single key (Updated)
by thanos1983 (Parson) on Aug 04, 2017 at 13:12 UTC

    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).

    Update2: In case you want to proceed in using module(s) you can simply use Text::CSV.

    Update3: Sample of *.csv file based on your description.

    Looking forward to your update, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Multiple values for a single key
by tybalt89 (Monsignor) on Aug 04, 2017 at 21:59 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1196722 use strict; use warnings; my %hash; while(<DATA>) { my ($key, $value) = ( /\w+/g )[0, -1]; push @{ $hash{$key} }, $value; } use Data::Dumper; print Dumper \%hash; __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

    Update

    The output is

    $VAR1 = { 'seetha' => [ 'rew' ], 'Anand' => [ 'xyz', 'wer', 'ert', 'tre' ] };
Re: Multiple values for a single key
by BillKSmith (Monsignor) on Aug 05, 2017 at 14:01 UTC
    Refer to the perl document: perldsc (Section "HASH OF ARRAYS")
    Bill
Re: Multiple values for a single key
by GotToBTru (Prior) on Aug 04, 2017 at 15:58 UTC

    Use code tags, please. It helps us help you. I can guess at the structure of your file but can't distinctively tell where the rows begin and end.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1196722]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-18 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found