Passing a hash by reference

From: Niall Macpherson (niall.macpherson_at_moneyline.com)
Date: 12/12/03


Date: 12 Dec 2003 09:02:29 -0800

Apologies if this has been covered before - I have seen a number of
posts on this subject but I still cannot work out my code doesn't
work.

I have been working with C / C++ for over 10 years and am fully
conversant with pointers and passing by reference in C. However ,
passing by reference in perl is causing me some problems.

Here is a little bit of test code -

##----------------------------------------------------------------------
sub AddHashEntries
{
        my($rh_hashref) = @_;
        for($cnt = 0; $cnt < 10; $cnt ++)
        {
                $value = "value " . $cnt;
                $key = "key" . $cnt;
                $$rh_hashref{$key} = $value;
        }
        return;
}
##----------------------------------------------------------------------
sub PrintHashEntries
{
        my($rh_hashref) = @_;
        
        foreach my $key (keys $$rh_hashref)
        {
                print("\nKey = [$key], value = $$rh_hashref{$key}");
        }
        return;
}
##----------------------------------------------------------------------
my %testhash = {};
AddHashEntries(\%testhash);
print("\nAdded all entries OK");
foreach my $lkey (keys %testhash) ## *** 1 ****
{
        print("\nKey = [$lkey], value = $testhash{$lkey}");
}
PrintHashEntries(\%testhash);
##----------------------------------------------------------------------

The values appear to get added to hash OK - the call at *** 1 ****
produces output as I would expect , eg

Added all entries OK
Key = [key7], value = value 7
Key = [HASH(0x1abefac)], value =
Key = [key8], value = value 8
Key = [key9], value = value 9
Key = [key0], value = value 0
Key = [key1], value = value 1
Key = [key2], value = value 2
Key = [key3], value = value 3
Key = [key4], value = value 4
Key = [key5], value = value 5
Key = [key6], value = value 6

However , I can't even get the PrintHashEntries routine to compile - I
get

Type of arg 1 to keys must be hash (not scalar dereference) at
C:\MoreTestStuff\
GMPriceConf\hashreftest.pl line 20, near "$rh_hashref)
        "
Execution of C:\MoreTestStuff\GMPriceConf\hashreftest.pl aborted due
to compilat
ion errors.
 

Can anyone tell me what I am doing wrong ?

Thanks



Relevant Pages

  • configurable device (and other) tables in the kernel ?
    ... containing at least device-id tables, ... Right now we compile them statically in the code. ... human-readable representation of entries in userland, ...
    (freebsd-arch)
  • Re: Multiple references to same component in reference dialog
    ... I have several entries for components I evidently ... I use RegClean on XP/SP2. ... My expperience is that if I compile a project with or without binary ... the entry will still be in the registry and in the ...
    (microsoft.public.vb.general.discussion)
  • Distinct list formula
    ... Does anyone know of a formula I could use to compile a distinct list of ... entries from another column. ... correponding values from some static data so it can be graphed. ... summergs's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=37419 ...
    (microsoft.public.excel.misc)
  • Re: multi-column data consolidation
    ... Not sure what 'add up or compile' means with text entries ... > How can I have the six months add up or compiled as a single column> database in another sheet or a separate column in that same sheet? ... I> will be adding data every so often under these months and it should> update the database. ...
    (microsoft.public.excel.misc)