N
Niall Macpherson
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
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