0

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)'
        ];
8
  • Re "I can’t figure out how to dereference the array references inside my array", Technically, are you are deferencing it. What output are you expecting? Commented Nov 25, 2015 at 19:29
  • Re "Don’t understand why @{$HoA{$cols[0]}} prints only the length of the array", @{$HoA{$cols[0]}} doesn't print anything. @a in 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. Commented Nov 25, 2015 at 19:31
  • what is your expected output? your join is treating array references as strings, but without expected output it is hard to know what you should be doing instead Commented Nov 25, 2015 at 19:40
  • I was expecting that the keys in my file were completely replaced by the corresponding arrays in the values of my hash of arrays, but what I got instead were the references pointing to those arrays. Probably not well explained, my apologies. Commented Nov 25, 2015 at 19:44
  • 1
    It's still hard to see exactly what you wanted the output to look like. Why not edit your post and add sections to show the complete "Expected Output" and "Actual Output"? Commented Nov 25, 2015 at 19:54

1 Answer 1

4

The big problem is this line:

    push ( @newarray, join( " ", @cols ));

join is inherently a string operation: @cols is an array of references, which join then dutifully stringifies so that it can join them with " ".

It seems like what you really want is probably this:

    push ( @newarray, [@cols] );

where the [ ... ] notation creates a new anonymous array (in this case populated with the values in @cols) and returns a reference to to it.

Additionally, instead of this:

    $cols[0] = $HoA{ $cols[0] };

(which causes @newarray and %HoA to end up containing references to the same underlying arrays), you may want this:

    $cols[0] = [ @{$HoA{ $cols[0] }} ];

(such that @newarray ends up with completely independent arrays that simply start out with the same data as what's in %HoA). This depends on whether you're planning to modify any of the arrays afterward.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that's exactly it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.