Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problems accessing a hash reference using each

Reply
Thread Tools

Problems accessing a hash reference using each

 
 
Niall Macpherson
Guest
Posts: n/a
 
      04-28-2004
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-28-2004
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

 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      04-28-2004
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-28-2004
Niall Macpherson <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Niall Macpherson
Guest
Posts: n/a
 
      04-29-2004
Mark Clements <(E-Mail Removed)> wrote in message news:<408f8d28$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Niall Macpherson
Guest
Posts: n/a
 
      04-29-2004
Tad McClellan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Niall Macpherson <(E-Mail Removed)> 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
 
Reply With Quote
 
Iain Chalmers
Guest
Posts: n/a
 
      04-29-2004
In article <(E-Mail Removed) >,
(E-Mail Removed) (Niall Macpherson) wrote:

> Tad McClellan <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)>...
> > Niall Macpherson <(E-Mail Removed)> 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
 
Reply With Quote
 
Richard Morse
Guest
Posts: n/a
 
      04-29-2004
In article <(E-Mail Removed) >,
(E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash .each and different action for each key Igor Nn Ruby 7 05-28-2011 12:33 PM
hash.each and hash.each_pair Hunt Jon Ruby 2 12-20-2008 03:07 PM
Hash#each vs Hash#each_pair Patrick Doyle Ruby 11 08-18-2008 04:52 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM



Advertisments