Getting Values from Hashes

Discussion in 'Perl Misc' started by George Kinley, Jun 18, 2004.

  1. Hi
    I have an hash as $Hash{Name}{NickName}
    Where Name is String
    NickName Array
    I can get All NickName in an array as

    @AllNN=values @{$$Hash{Name}}

    my query is Why I cant get the same result with
    @AllNN=values %{$Hash{Name}}
    or
    @AllNN=values $Hash{Name}




    --
     
    George Kinley, Jun 18, 2004
    #1
    1. Advertising

  2. George Kinley

    Paul Lalli Guest

    On Fri, 18 Jun 2004, George Kinley wrote:

    > Hi
    > I have an hash as $Hash{Name}{NickName}
    > Where Name is String
    > NickName Array
    > I can get All NickName in an array as
    >
    > @AllNN=values @{$$Hash{Name}}
    >
    > my query is Why I cant get the same result with
    > @AllNN=values %{$Hash{Name}}
    > or
    > @AllNN=values $Hash{Name}
    >


    Analize your datastructure. It looks something like this, yes?

    %Hash = (
    'Robert' => ['Bob', 'Rob'],
    'William' => ['Will', 'Billy'],
    );


    Now analize the code you were trying to use.

    values %{$Hash{'Robert'}}

    $Hash{'Robert'} is an array reference. It references an array containing
    Bob and Rob. You cannot derefernce this as a hash because it is not a
    hash reference.

    values $Hash{'Robert'}

    $Hash{'Robert'} is an array reference. You cannot use values on an array
    reference. The argument to values() must be a hash.

    I don't quite understand your claim that values @{$$Hash{Name}} works like
    you want it. Even if I have misunderstood your datastructure, you cannot
    pass an array to values(). This is a syntax error.

    Using the datastructure I typed above, you would get all the nicknames for
    a particular name like this:
    @nicks = @{$Hash{'Robert'}};

    If you want *all* nicknames, you could use something like this:

    @all_nicks = map { @{$Hash{$_}} } keys %Hash;



    If you need further assistance (if, for example, I have not understood
    your description of your datastructure), please post a short but complete
    program demonstrating your issue.

    Thank you,
    Paul Lalli
     
    Paul Lalli, Jun 18, 2004
    #2
    1. Advertising

  3. Re: Getting Values from Hashes

    Paul Lalli wrote:

    > On Fri, 18 Jun 2004, George Kinley wrote:
    >
    > > Hi
    > > I have an hash as $Hash{Name}{NickName}
    > > Where Name is String
    > > NickName Array
    > > I can get All NickName in an array as
    > >
    > > @AllNN=values @{$$Hash{Name}}
    > >
    > > my query is Why I cant get the same result with
    > > @AllNN=values %{$Hash{Name}}
    > > or
    > > @AllNN=values $Hash{Name}
    > >

    >
    > Analize your datastructure. It looks something like this, yes?
    >
    > %Hash = (
    > 'Robert' => ['Bob', 'Rob'],
    > 'William' => ['Will', 'Billy'],
    > );
    >
    >
    > Now analize the code you were trying to use.
    >
    > values %{$Hash{'Robert'}}
    >
    > $Hash{'Robert'} is an array reference. It references an array
    > containing Bob and Rob. You cannot derefernce this as a hash because
    > it is not a hash reference.
    >
    > values $Hash{'Robert'}
    >
    > $Hash{'Robert'} is an array reference. You cannot use values on an
    > array reference. The argument to values() must be a hash.
    >
    > I don't quite understand your claim that values @{$$Hash{Name}} works
    > like you want it. Even if I have misunderstood your datastructure,
    > you cannot pass an array to values(). This is a syntax error.
    >
    > Using the datastructure I typed above, you would get all the
    > nicknames for a particular name like this:
    > @nicks = @{$Hash{'Robert'}};
    >
    > If you want all nicknames, you could use something like this:
    >
    > @all_nicks = map { @{$Hash{$_}} } keys %Hash;
    >
    >
    >
    > If you need further assistance (if, for example, I have not understood
    > your description of your datastructure), please post a short but
    > complete program demonstrating your issue.
    >
    > Thank you,
    > Paul Lalli



    I believe its my mistake that I did not mentioned that I was getting
    this HASH as reference from another procedure, so that is the reason I
    need to dereference it by double "$"
    --
     
    George Kinley, Jun 18, 2004
    #3
  4. George Kinley

    Paul Lalli Guest

    Re: Getting Values from Hashes

    On Fri, 18 Jun 2004, George Kinley wrote:

    > Paul Lalli wrote:
    >
    > > On Fri, 18 Jun 2004, George Kinley wrote:
    > >
    > > > Hi
    > > > I have an hash as $Hash{Name}{NickName}
    > > > Where Name is String
    > > > NickName Array
    > > > I can get All NickName in an array as
    > > >
    > > > @AllNN=values @{$$Hash{Name}}
    > > >
    > > > my query is Why I cant get the same result with
    > > > @AllNN=values %{$Hash{Name}}
    > > > or
    > > > @AllNN=values $Hash{Name}
    > > >

    > >
    > > I don't quite understand your claim that values @{$$Hash{Name}} works
    > > like you want it. Even if I have misunderstood your datastructure,
    > > you cannot pass an array to values(). This is a syntax error.
    > >

    >
    > I believe its my mistake that I did not mentioned that I was getting
    > this HASH as reference from another procedure, so that is the reason I
    > need to dereference it by double "$"



    This is not at all the issue. You're claiming that you're passing an
    array (something that starts with '@') to the values() function. This
    doesn't work. It's a syntax error. It doesn't matter how many levels of
    dereferencing are involved.

    Post a short but complete program that illustrates your problem if you're
    still having one.

    Paul Lalli
     
    Paul Lalli, Jun 18, 2004
    #4
  5. Re: Getting Values from Hashes

    Paul Lalli wrote:

    > On Fri, 18 Jun 2004, George Kinley wrote:
    >
    > > Paul Lalli wrote:
    > >
    > > > On Fri, 18 Jun 2004, George Kinley wrote:
    > > >
    > > > > Hi
    > > > > I have an hash as $Hash{Name}{NickName}
    > > > > Where Name is String
    > > > > NickName Array
    > > > > I can get All NickName in an array as
    > > > >
    > > > > @AllNN=values @{$$Hash{Name}}
    > > > >
    > > > > my query is Why I cant get the same result with
    > > > > @AllNN=values %{$Hash{Name}}
    > > > > or
    > > > > @AllNN=values $Hash{Name}
    > > > >
    > > >
    > > > I don't quite understand your claim that values @{$$Hash{Name}}
    > > > works like you want it. Even if I have misunderstood your
    > > > datastructure, you cannot pass an array to values(). This is a
    > > > syntax error.
    > > >

    > >
    > > I believe its my mistake that I did not mentioned that I was getting
    > > this HASH as reference from another procedure, so that is the
    > > reason I need to dereference it by double "$"

    >
    >
    > This is not at all the issue. You're claiming that you're passing an
    > array (something that starts with '@') to the values() function. This
    > doesn't work. It's a syntax error. It doesn't matter how many
    > levels of dereferencing are involved.
    >
    > Post a short but complete program that illustrates your problem if
    > you're still having one.
    >
    > Paul Lalli


    SOrry for asking the wrong question I was also not using the "values"
    Sorry again


    But my questions was

    my query is Why I cant get the same result with
    > > > > @AllNN=values %{$Hash{Name}}
    > > > > or
    > > > > @AllNN=values $Hash{Name}
    > > > >


    that was explained by you thanks
    --
    -Gk
     
    George Kinley, Jun 21, 2004
    #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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    566
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    161
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    277
    Matthias Wächter
    Sep 13, 2007
  4. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    242
    Markus Schirp
    May 28, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    214
Loading...

Share This Page