Problems accessing a hash reference using each

Discussion in 'Perl Misc' started by Niall Macpherson, Apr 28, 2004.

  1. I'm a bit of a newbie to using references and am having a problem
    dereferencing a hash

    I have a subroutine which either adds a value to hash or prints out a
    value from the hash.

    I can add a value OK , but I have problems retrieving the value using
    the each command. Here's a cut down version of the code

    sub GetOrAddValue
    {
    my($rh_list, $date, $add, $key) = @_;

    if($add)
    {
    $rh_tablelist->{$key}{"date"} = $date;
    }
    else
    {
    my($hashkey, $value)
    while(($hashkey, $value) = each (%rh_list))
    {
    # This works but looks a bit messy
    print("\nGot $rh_tablelist->{$hashkey}{\"date\"} ");

    # This doesn't work - if I print $value it tells me that
    # it is a hash so I assumed I should be able to do this
    # print("\nGot $value{\"date\"}")
    }
    }

    Can someone tell me how I use $value ?

    Thanks
     
    Niall Macpherson, Apr 28, 2004
    #1
    1. Advertising

  2. Niall Macpherson wrote:
    > I'm a bit of a newbie to using references and am having a problem
    > dereferencing a hash
    >
    > I have a subroutine which either adds a value to hash or prints out
    > a value from the hash.
    >
    > I can add a value OK , but I have problems retrieving the value
    > using the each command. Here's a cut down version of the code
    >
    > sub GetOrAddValue
    > {
    > my($rh_list, $date, $add, $key) = @_;
    >
    > if($add)
    > {
    > $rh_tablelist->{$key}{"date"} = $date;
    > }
    > else
    > {
    > my($hashkey, $value)
    > while(($hashkey, $value) = each (%rh_list))
    > {
    > # This works but looks a bit messy
    > print("\nGot $rh_tablelist->{$hashkey}{\"date\"} ");
    >
    > # This doesn't work - if I print $value it tells me that
    > # it is a hash so I assumed I should be able to do this
    > # print("\nGot $value{\"date\"}")
    > }
    > }
    >
    > Can someone tell me how I use $value ?


    Please post a short but *complete* program that illustrates what you
    are trying to do, what the expected output is, and the *exact* actual
    output.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 28, 2004
    #2
    1. Advertising

  3. Niall Macpherson wrote:

    > I'm a bit of a newbie to using references and am having a problem
    > dereferencing a hash


    > sub GetOrAddValue
    > {
    > my($rh_list, $date, $add, $key) = @_;

    <snip>

    > while(($hashkey, $value) = each (%rh_list))

    The proglet doesn't compile, for a start: you're missing a close-brace
    and a semi-colon. One of the problems here (apart from not using strict
    and warnings, which would have given you some hint as to what was
    wrong), is that you are passing in $rh_list (ie a scalar) and then
    executing each on %rh_list, which is a different variable. You may have
    more luck with %{$rh_list} ie casting the reference to a hash first.



    > print("\nGot $rh_tablelist->{$hashkey}{\"date\"} ");

    I take it $rh_tablelist is a global. Yuk. You could use single quotes
    for the key ie $rh_tablelist->{$hashkey}->{'date'} rather than escaping
    double quotes, or you can omit the quotes altogether ie
    $rh_tablelist->{$hashkey}->{date}.

    >print("\nGot $value{\"date\"}")


    $value is probably a hashref, not a hash (values of associative arrays
    are scalars). you need $value->{date}.

    Mark
     
    Mark Clements, Apr 28, 2004
    #3
  4. Niall Macpherson <> wrote:

    > I'm a bit of a newbie to using references and am having a problem
    > dereferencing a hash



    Have you read the tutorial about references yet?

    perldoc perlreftut


    > I have problems retrieving the value using
    > the each command.


    > while(($hashkey, $value) = each (%rh_list))


    > Can someone tell me how I use $value ?



    Apply "Use Rule 1" from the above-mentioned std doc:

    while( my($hashkey, $value) = each %{ $rh_list } )


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 28, 2004
    #4
  5. Mark Clements <> wrote in message news:<408f8d28$>...
    > The proglet doesn't compile, for a start: you're missing a close-brace
    > and a semi-colon. One of the problems here (apart from not using strict
    > and warnings, which would have given you some hint as to what was
    > wrong), > Mark



    Sorry about that - the complete prog is about 600 - 700 lines so I
    just cut and pasted the function removes some irrelevant bits from it
    and screwed up the cut and paste slightly. Should have put it in a
    separate script and tried to run int beffer sending - sorry :(

    I was using strict and warnings and I actually got

    Global symbol "%value" requires explicit package name at ....

    for the line

    print("\nGot $value{\"date\"}");

    which wouldn't compile.

    Anyway thanks for your help - think I am on the right track now
     
    Niall Macpherson, Apr 29, 2004
    #5
  6. Tad McClellan <> wrote in message news:<>...
    > Niall Macpherson <> wrote:
    >
    > > I'm a bit of a newbie to using references and am having a problem
    > > dereferencing a hash

    >
    >
    > Have you read the tutorial about references yet?
    >
    > perldoc perlreftut
    >

    Thanks - that sounds like a very good idea

    I'm afraid I've been tending to use the O'Reilly perl CD Bookshelf
    rather than perldoc . The main problem I have with perldoc is figuring
    out what the names of the modules are. How would I have known that
    the page for perlreftut existed ?

    Is there somewhere I can get a list of the available pages / modules
    from perldoc ?

    perldoc perldoc doesn't seem to give me the answer.

    Thanks
     
    Niall Macpherson, Apr 29, 2004
    #6
  7. In article <>,
    (Niall Macpherson) wrote:

    > Tad McClellan <> wrote in message
    > news:<>...
    > > Niall Macpherson <> wrote:
    > >
    > > > I'm a bit of a newbie to using references and am having a problem
    > > > dereferencing a hash

    > >
    > >
    > > Have you read the tutorial about references yet?
    > >
    > > perldoc perlreftut
    > >

    > Thanks - that sounds like a very good idea
    >
    > I'm afraid I've been tending to use the O'Reilly perl CD Bookshelf
    > rather than perldoc . The main problem I have with perldoc is figuring
    > out what the names of the modules are. How would I have known that
    > the page for perlreftut existed ?
    >
    > Is there somewhere I can get a list of the available pages / modules
    > from perldoc ?
    >
    > perldoc perldoc doesn't seem to give me the answer.


    You were _so_ close!

    try perldoc perl

    big
    --
    "A magazine here has a bunch of bonobo's in the zoo and a bunch of
    market analysts from major banks picking stocks weekly. So far the
    bonobo's are turning in a profit and the analysts lose money. The
    banks haven't made the logical step yet." - Lieven Marchand in SDM
     
    Iain Chalmers, Apr 29, 2004
    #7
  8. In article <>,
    (Niall Macpherson) wrote:

    > I was using strict and warnings and I actually got
    >
    > Global symbol "%value" requires explicit package name at ....
    >
    > for the line
    >
    > print("\nGot $value{\"date\"}");
    >
    > which wouldn't compile.


    Try:
    print "\nGot $value{'date'}";

    or
    print "\nGot $value{date}";

    Also, of course, may sure you declared %value with either 'my' or 'our'.

    HTH,
    Ricky

    --
    Pukku
     
    Richard Morse, Apr 29, 2004
    #8
    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. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    629
    David A. Black
    Jul 2, 2008
  3. Patrick Doyle

    Hash#each vs Hash#each_pair

    Patrick Doyle, Aug 18, 2008, in forum: Ruby
    Replies:
    11
    Views:
    238
    Michael Fellinger
    Aug 18, 2008
  4. Hunt Jon

    hash.each and hash.each_pair

    Hunt Jon, Dec 18, 2008, in forum: Ruby
    Replies:
    2
    Views:
    351
    Simon Krahnke
    Dec 20, 2008
  5. Igor Nn
    Replies:
    7
    Views:
    437
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page