on Mon, 22 Apr 2002 06:18:01 GMT, [EMAIL PROTECTED] (Hans
Holtan) wrote:
> Hi folks,
> The code shown bellow is supposed to de-reference a hash-reference
> of array-references and print everything out nicely. But the array
> references do not get de-referenced. I really appreciate the
> help. Thanks,
> Hans
I tried to reduce your code to the essential part you have a problem
with:
#! perl -w
use strict;
sub read_alignments {
my $teststring = 'ABCDEFG';
my %alignments = ();
$alignments{testkey} = [split //, $teststring];
return \%alignments;
}
my $alignment_hash = read_alignments();
foreach my $key (keys %$alignment_hash) {
print "{$key}\t=>\t[$$alignment_hash{$key}}]\n";
}
This prints:
{testkey} => [ARRAY(0x1775578)}]
which indeed doesn't dereference the array. The problem is in the
following expression:
$$alignment_hash{$key}
which dereferences a hash which itself contains references to
arrays as its values.
This is better written (by convention) as
$alignment_hash->{$key}
To dereference this array reference, you have to write
@{$alignment_hash->{$key}}
Note that the extra {} are necessary here for precedence reasons. If
you omit them, perl thinks you mean
{@$alignment_hash}->{$key}
which gives you a 'Not an ARRAY reference'-error.
--
felix
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]