0

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?

2
  • Have you read perldoc perldsc and perldoc perllol? Some very similar structures are dissected in detail there. Commented Nov 11, 2010 at 0:12
  • Also perlmonks.org/?node=References+quick+reference - though you seem to get the idea, but have a slight difference between your data and what you seem to expect. Commented Nov 11, 2010 at 3:01

3 Answers 3

1

$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.

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

1 Comment

Though $HoHoA{$group}{$suite}[1] is an arrayref.
1

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. ☺

Comments

0

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);

Comments

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.