Perl: size of array in a hash, in another hash

Question!

So, I have a hash %HoHoA. Each top level hash key has a second level hash key as a value. Each second level hash has arrays-of-arrays as values.

In perl's debugger it looks something like this:

0 'Top_key_1'
1 HASH(0x...)
  'Second_Key_1' => ARRAY(0x...)
    0   'string 1'
    1   'string 2'
  'Second_Key_2' => ARRAY(0x...)
    0  ARRAY(0x...)
      0 'string 3'
      1 'string 4'
      2 'string 5'
    1  ARRAY(0x...)
      0 'string 6'
      1 'string 7'
2 'Top_key_2'

I'm trying to get the size of each suite's two arrays. In the above example, Second_Key_2 has two arrays, the 0th one is size 3.

my $count1 = $#{$HoHoA{$top_key}{$second_key}[0]}+1;
my $count2 = $#{$HoHoA{$top_key}{$second_key}[1]}+1;

and

my $count1 = @{$HoHoA{$group}{$suite}[0]};
my $count2 = @{$HoHoA{$group}{$suite}[1]};

I get an error message like: Can't use string ("string 3") as an ARRAY ref while "strict refs" in use

Why am I getting that error message, and what should I do instead?

By : jasper77


Answers

It look like you went one level too deep. The code below should get you what you want

my $count1 = @{$HoHoA{$group}{$suite}};

You may also may want to use Data::Dumper to see the Structure of your object to ensure you are working on the write data format. use Data::Dumper; print Dumper($HoHoA);

By : James


$HoHoA{$group}{$suite}[0] isn't an arrayref; it's a string, thus the error. Maybe you need to debug the code that's building your data structure.

By : hobbs


Run it under the debugger and recursively dump out a data structure, or point therein, with the x command.

You can do that programmatically with the Dumpvalue module, but it’s much less convenient.

I wish people wouldn’t keep thinking I wrote perllol as a joke. ?

By : tchrist


This video can help you solving your question :)
By: admin