Perl data structure to hold same multiple keys and different values

Discussion in 'Perl Misc' started by Ami, Sep 17, 2007.

  1. Ami

    Ami Guest

    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
     
    Ami, Sep 17, 2007
    #1
    1. Advertising

  2. Ami

    Ami Guest

    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
     
    Ami, Sep 17, 2007
    #2
    1. Advertising

  3. Ami

    kens Guest

    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
     
    kens, Sep 17, 2007
    #3
  4. Ami

    Mumia W. Guest

    Re: Perl data structure to hold same multiple keys and differentvalues

    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
     
    Mumia W., Sep 17, 2007
    #4
  5. Ami

    Ami Guest

    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
     
    Ami, Sep 18, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    729
    Daniel T.
    Feb 3, 2006
  2. Replies:
    14
    Views:
    453
    Jerry Coffin
    Feb 17, 2008
  3. Christopher Brewster
    Replies:
    5
    Views:
    347
    John Machin
    Nov 14, 2008
  4. bluebaron
    Replies:
    3
    Views:
    747
    Jonathan N. Little
    Nov 4, 2009
  5. A
    Replies:
    27
    Views:
    1,602
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page