Perl data structure to hold same multiple keys and different values

A

Ami

Hi All,
I am quite new to perl and need to write a script which can hold same
keys for multiple different associated values/object. Can any one help
me to find such data structure, where I can put these keys and sort
them on the keys and get the same key group to access different values/
object.
Thanks in advance for help.
-Ami
 
A

Ami

may be i am not clear with my question so once again:
I need a hash kind of data structure where I can store multiple values/
objects with same key value. I need to create a group on base of same
key. Associated values/objects will be processed on the base of groups
created.
Any help is appreciated.
Thanks
 
K

kens

may be i am not clear with my question so once again:
I need a hash kind of data structure where I can store multiple values/
objects with same key value. I need to create a group on base of same
key. Associated values/objects will be processed on the base of groups
created.
Any help is appreciated.
Thanks

Hello. It sounds like you may want a hash of arrays. I have a simple
example below. See if this is something close to what you want.

use strict;
use warnings;

my %hashOfArrays;


foreach my $friend ( 'James', 'John', 'etc' )
{
push @{$hashOfArrays{'Ami'}}, $friend;
}

foreach my $name ( sort keys %hashOfArrays )
{
print "$name\'s friends:\n";
foreach my $friend ( @{$hashOfArrays{$name}} )
{
print " $friend\n";
}
}

Each element of the array could be a reference to an object if so
desired.

HTH, Ken
 
M

Mumia W.

may be i am not clear with my question so once again:
I need a hash kind of data structure where I can store multiple values/
objects with same key value. I need to create a group on base of same
key. Associated values/objects will be processed on the base of groups
created.
Any help is appreciated.
Thanks

Read the data structures introduction:

http://perldoc.perl.org/perldsc.html
 
A

Ami

Hello. It sounds like you may want a hash of arrays. I have a simple
example below. See if this is something close to what you want.

use strict;
use warnings;

my %hashOfArrays;

foreach my $friend ( 'James', 'John', 'etc' )
{
push @{$hashOfArrays{'Ami'}}, $friend;

}

foreach my $name ( sort keys %hashOfArrays )
{
print "$name\'s friends:\n";
foreach my $friend ( @{$hashOfArrays{$name}} )
{
print " $friend\n";
}

}

Each element of the array could be a reference to an object if so
desired.

HTH, Ken

Hi Ken,
Many thanks for your help and example code. It is exactly same what
i wanted.
Thanks again,
-Ami
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top