Home: Perl Programming Help: Advanced:
Derefenced a hash of pointers to hashes


Jul 3, 2001, 7:03 AM

Views: 2589
Derefenced a hash of pointers to hashes

I get wrong output trying to derefenced an pointer to a hash which is a value of a key in another hash.
Here is the codes and output.
my $lineread = readline($fhandle);

my @gsec_codes = parse_str($lineread);
splice(@gsec_codes, 0,1);

#Gensec codes pointer
my $gensec_ptr = \@gsec_codes;
$self->{_map_tab_gcodes} = $gensec_ptr;

$idx = 0;

#Read the second line which is the first row of data
$lineread = readline($fhandle);

#Check for EOF condition - i.e. NULL line content
while ($lineread ne "")

print "\nLine $idx before parse: = $lineread\n";
my @pen_codes = parse_str($lineread);
splice(@pen_codes, 0,1);

my $pencode_ptr = \@pen_codes;
local @values=($gensec_ptr,$pencode_ptr);
my %gsec = make_gensec_hash(@values);

#hash pointer to the gensec fund data & pen code data for that fund
my $map_ptr = \%gsec;

my $firstcomma = index($lineread,",",0);
my $fund_code = substr($lineread,0,$firstcomma);

#Make a hash of the fund code and a pointer(to hash of the pencode data)
$map_hash{"$fund_code"} = "$map_ptr";


$lineread = readline($fhandle);


my @keysmap=keys(%map_hash);
print "map_hash keys=@keysmap\n";

my $map_table_ptr = \%map_hash;
print "map_table_ptr=$map_table_ptr\n";

#Set the data attribute to the pointer of the map data
$self->{_map_tab_data} = $map_table_ptr;


sub make_gensec_hash
#Make a hash of the gensec codes and pen codes data

my $gsec_ptr = $_[0];
my $pcode_ptr = $_[1];

my @gcodes = @$gsec_ptr;
my @pcodes = @$pcode_ptr;

my $gsec_hash = {};

for ($idx4 =0; $idx4 < $#gcodes+1; $idx4++)
print "gcode=$gcodes[$idx4] pcode=$pcodes[$idx4]\n";
$gsec_hash{"$gcodes[$idx4]"} = "$pcodes[$idx4]";

return %gsec_hash;


sub map_values
#Map the dbset data to the map table and returns a pointer to the
#mapped data.

my $class = shift @_;

my $dbset = $_[0]; #$self->{_dbset};
my $mapset = $self->{_map_tab_data};
my $gcode = $self->{_map_tab_gcodes};

print "\nMap values:\n";

my %maptab = %$mapset;
my @keysmap = keys(%maptab);
print "keysmap=@keysmap\n";

my $idx = 0;

my @keysdbset = keys(%{$$dbset[$idx]});

print "keysdbset=@keysdbset\n";

for ($idx1 = 0;$idx1 < $#$dbset+1; $idx1++)
my $key_fund_val = %{$$dbset[$idx1]}->{CONT_NO};
print "db set fund val=$key_fund_val\n";

my $key_fd_id = %{$$dbset[$idx1]}->{FD_DESC_ID};
print "db set fd_id val=$key_fd_id\n";

#Value of the fund in the mapped table is a pointer to a hash.
my $mapval = $maptab{$key_fund_val};

print "keyval=$key_fund_val mapval=$mapval\n";

my %testhash = %$mapval;

while ( ($key,$value) = each(%hash) )
print "loop thru hash=$key $value\n";


Output map_values():
Map values:
keysmap=S00523 S00501 S00510
db set fund val=S00523
db set fd_id val=0001
keyval=S00523 mapval=HASH(0x24b0d24)
db set fund val=S00501
db set fd_id val=0020
keyval=S00501 mapval=HASH(0x24b0d24)
db set fund val=S00510
db set fd_id val=0020
keyval=S00510 mapval=HASH(0x24b0d24)

The part of the WHILE LOOP in map_values()
is not executed, as the hash that is dereferenced
into my %testhash = %$mapval;
contain no keys & values?? how do I dereferenced
that I can display this hash as indicated
as -> HASH(0x24b0d24) which is clealry a pointer
to a hash. The code
my %testhash = %$mapval;
surelu looks normally correct in the way to dereferenced a hash.??

As well as why does the hash pointer have the same
memeory address it should be different for each
hash as there are three hashes created in the loop
of pencodes_ptr

Please help.

from Francois

Administrator / Moderator

Jul 3, 2001, 8:00 AM

Views: 2585
Re: Derefenced a hash of pointers to hashes

See mhx's reply at [url=http://forums.perlguru.com/showflat.pl?Board=Forum7&Number=11325.

Francois, please post your question in only *1* forum. Duplicate posts are deleted.