I’ve read the reference documentation, but I can’t figure out how to dereference the array references inside my array.
Don’t understand why @{$HoA{$cols[0]}} prints only the length of the array either. Any clarification is much appreciated.
file.txt:
aa bb
bb cc
The program:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# my $filename = "file.txt";
my @newarray;
my %HoA = (
aa => [ "GAT_1", "GAT_2", "GAT_3", "GAT_4" ],
bb => [ "GAT_6", "GAT_1", "GAT_5", "GAT_4", "GAT_2" ],
cc => [ "GAT_6", "GAT_4", "GAT_3", "GAT_1", "GAT_2" ],
);
open (FILE, '<' ,"$filename") or print "$filename does not exist\n";
while (<FILE>) {
my @cols = split;
$cols[0] = $HoA{ $cols[0] };
#$cols[0] = @{$HoA{ $cols[0]} };
$cols[1] = $HoA{ $cols[1] };
#$cols[1] = @{$HoA{ $cols[1] }};
push ( @newarray, join( " ", @cols ));
}
close FILE;
print Dumper(\@newarray);
This is my expected output:
$VAR1 = [
[
[
'GAT_1',
'GAT_2',
'GAT_3',
'GAT_4'
],
[
'GAT_6',
'GAT_1',
'GAT_5',
'GAT_4',
'GAT_2'
],
[
'GAT_6',
'GAT_1',
'GAT_5',
'GAT_4',
'GAT_2'
],
[
'GAT_6',
'GAT_4',
'GAT_3',
'GAT_1',
'GAT_2'
],
]
];
This is my actual output:
$VAR1 = [
'ARRAY(0x7f80110060e8) ARRAY(0x7f801102eb58)',
'ARRAY(0x7f801102eb58) ARRAY(0x7f801102f308)'
];
@{$HoA{$cols[0]}}prints only the length of the array",@{$HoA{$cols[0]}}doesn't print anything.@ain scalar context evaluates to the number of elements in the array. As such,@{ REF }in scalar context evaluates to the number of elements in the referenced array.