Perl data structure to hold same multiple keys and different values
Perl data structure to hold same multiple keys and different values
am 17.09.2007 12:38:48 von 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
Re: Perl data structure to hold same multiple keys and different values
am 17.09.2007 12:42:35 von 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
Re: Perl data structure to hold same multiple keys and different values
am 17.09.2007 13:25:48 von kenslaterpa
On Sep 17, 6:42 am, Ami wrote:
> 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
Re: Perl data structure to hold same multiple keys and differentvalues
am 17.09.2007 15:17:46 von paduille.4061.mumia.w+nospam
On 09/17/2007 05:42 AM, Ami wrote:
> 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
Re: Perl data structure to hold same multiple keys and different values
am 18.09.2007 12:55:48 von AMI
On Sep 17, 4:25 pm, kens wrote:
> On Sep 17, 6:42 am, Ami wrote:
>
> > 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
Hi Ken,
Many thanks for your help and example code. It is exactly same what
i wanted.
Thanks again,
-Ami