S
Scott
All:
I am using Perl 5.8.0 on Red Hat Linux release 9 (Shrike).
I am attempting to pass a reference to an array whose elements are a
hash. When I go to generalize my code and pass a reference to this
array to a subroutine, the results I get back are unexpected.
Here is my annotated test program:
#!/usr/bin/perl -w
#Create empty array arySTUFF.
my @arySTUFF=();
#Create hash to map 0,1,2 to data element names
# corresponding to database column names.
my %hSTUFF_VARLIST=(0=>'DATE_AUTH',
1=>'FILENAME',
2=>'CLAIMS');
#Push an anonymous hash into the array arySTUFF
# making it the first (zeroth) element of the array.
# Note that I am initializing each of the hash elements
# in case there is no data coming in from my data source
# I will have some value.
push(@arySTUFF,{DATE_AUTH => 'XXXXXXXX',
FILENAME => 'No FileName',
CLAIMS => -1});
#Initialize the three hash elements for this first array element
# and then print out the results to see if it worked.
$$arySTUFF[0]->{$hSTUFF_VARLIST{0}}="20041116";
$$arySTUFF[0]->{$hSTUFF_VARLIST{1}}="BIGDATAFILE.TXT";
$$arySTUFF[0]->{$hSTUFF_VARLIST{2}}=12345;
print "DATE_AUTH=",$$arySTUFF[0]{$hSTUFF_VARLIST{0}},"\n";
print "FILENAME=",$$arySTUFF[0]{$hSTUFF_VARLIST{1}},"\n";
print "CLAIMS=",$$arySTUFF[0]{$hSTUFF_VARLIST{2}},"\n";
#The next line shows that $$arySTUFF[0] is a HASH reference
# at some memory location.
print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
#The next line shows the actual value in the hash reference
# for FILENAME. This works.
print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
#Pass a reference to the array arySTUFF and hash hSTUFF_VARLIST.
# Note that I have no problem with hash ref at all, just the array
ref.
print "===TRYSUB===\n";
TrySub(\@arySTUFF,\%hSTUFF_VARLIST);
#Use a subroutine to generalize the arrays and the hashes.
sub TrySub {
my @aryData = @{$_[0]};
my $hData = $_[1];
print "Num Rows==>",$#aryData+1,"\n";
#The following line show a difference HASH memory location
# than the one above.
print "ARYDATA[0]=",$aryData[0],"\n";
#The following prints out the initialization value "No Filename"
# rather than the expected BIGDATAFILE.TXT.
print "ARYDATA[0]=>",$aryData[0]->{FILENAME},"\n";
#Just in case, let's check if arySTUFF is messed up...it isn't.
print "\nCheck: Original arySTUFF is okay\n";
print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
}
exit;
Here are the results of this test program:
DATE_AUTH=20041116
FILENAME=BIGDATAFILE.TXT
CLAIMS=12345
ARYSTUFF[0]=HASH(0x80660b0)
ARYSTUFF[0]=>BIGDATAFILE.TXT
===TRYSUB===
Num Rows==>1
ARYDATA[0]=HASH(0x804ca88)
ARYDATA[0]=>No FileName
Check: Original arySTUFF is okay
ARYSTUFF[0]=HASH(0x80660b0)
ARYSTUFF[0]=>BIGDATAFILE.TXT
When the subroutine prints out "No Filename", I am confused as to why
this happens. I am expecting to see BIGDATAFILE.TXT. Any help with
this would be very greatly appreciated. (I'd hate to hard code my
program when a nice subroutine would be better!)
Thanks!
Scott
I am using Perl 5.8.0 on Red Hat Linux release 9 (Shrike).
I am attempting to pass a reference to an array whose elements are a
hash. When I go to generalize my code and pass a reference to this
array to a subroutine, the results I get back are unexpected.
Here is my annotated test program:
#!/usr/bin/perl -w
#Create empty array arySTUFF.
my @arySTUFF=();
#Create hash to map 0,1,2 to data element names
# corresponding to database column names.
my %hSTUFF_VARLIST=(0=>'DATE_AUTH',
1=>'FILENAME',
2=>'CLAIMS');
#Push an anonymous hash into the array arySTUFF
# making it the first (zeroth) element of the array.
# Note that I am initializing each of the hash elements
# in case there is no data coming in from my data source
# I will have some value.
push(@arySTUFF,{DATE_AUTH => 'XXXXXXXX',
FILENAME => 'No FileName',
CLAIMS => -1});
#Initialize the three hash elements for this first array element
# and then print out the results to see if it worked.
$$arySTUFF[0]->{$hSTUFF_VARLIST{0}}="20041116";
$$arySTUFF[0]->{$hSTUFF_VARLIST{1}}="BIGDATAFILE.TXT";
$$arySTUFF[0]->{$hSTUFF_VARLIST{2}}=12345;
print "DATE_AUTH=",$$arySTUFF[0]{$hSTUFF_VARLIST{0}},"\n";
print "FILENAME=",$$arySTUFF[0]{$hSTUFF_VARLIST{1}},"\n";
print "CLAIMS=",$$arySTUFF[0]{$hSTUFF_VARLIST{2}},"\n";
#The next line shows that $$arySTUFF[0] is a HASH reference
# at some memory location.
print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
#The next line shows the actual value in the hash reference
# for FILENAME. This works.
print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
#Pass a reference to the array arySTUFF and hash hSTUFF_VARLIST.
# Note that I have no problem with hash ref at all, just the array
ref.
print "===TRYSUB===\n";
TrySub(\@arySTUFF,\%hSTUFF_VARLIST);
#Use a subroutine to generalize the arrays and the hashes.
sub TrySub {
my @aryData = @{$_[0]};
my $hData = $_[1];
print "Num Rows==>",$#aryData+1,"\n";
#The following line show a difference HASH memory location
# than the one above.
print "ARYDATA[0]=",$aryData[0],"\n";
#The following prints out the initialization value "No Filename"
# rather than the expected BIGDATAFILE.TXT.
print "ARYDATA[0]=>",$aryData[0]->{FILENAME},"\n";
#Just in case, let's check if arySTUFF is messed up...it isn't.
print "\nCheck: Original arySTUFF is okay\n";
print "ARYSTUFF[0]=",$$arySTUFF[0],"\n";
print "ARYSTUFF[0]=>",$$arySTUFF[0]->{FILENAME},"\n";
}
exit;
Here are the results of this test program:
DATE_AUTH=20041116
FILENAME=BIGDATAFILE.TXT
CLAIMS=12345
ARYSTUFF[0]=HASH(0x80660b0)
ARYSTUFF[0]=>BIGDATAFILE.TXT
===TRYSUB===
Num Rows==>1
ARYDATA[0]=HASH(0x804ca88)
ARYDATA[0]=>No FileName
Check: Original arySTUFF is okay
ARYSTUFF[0]=HASH(0x80660b0)
ARYSTUFF[0]=>BIGDATAFILE.TXT
When the subroutine prints out "No Filename", I am confused as to why
this happens. I am expecting to see BIGDATAFILE.TXT. Any help with
this would be very greatly appreciated. (I'd hate to hard code my
program when a nice subroutine would be better!)
Thanks!
Scott