in reply to Array of hashes reference problem

Where is $j defined? What's it's value? It looks like $arrayAoH[$j] contains an empty string, not a hash ref.

the lowliest monk

Replies are listed 'Best First'.
Re^2: Array of hashes reference problem
by tlemons (Novice) on Jun 22, 2005 at 17:52 UTC
    Sorry, here's the whole code segment:

    our @arrayAoH = ""; my $i = 0; # i holds the line number of the input (@discovered) ar +ray. my $j = 0; # j holds the number of the table row. my $key; my $value; # $# returns the subscript of the last element in the array. until ($i eq $#discovered){ if ($discovered[$i] =~ m/Item Number:/){ # parse this line containing labels and values until a blank line is f +ound until ($discovered[$i] eq ""){ ($key, $value) = split /:\s*/, $discovered[$i]; ## $arrayAoH[$j] = { $key => $value }; $arrayAoH[$j]{$key} = $value; $i++; } # If the selected item shouldn't be processed, remove this array row. if (($arrayAoH[$j]{"Model Number"} !~ m/^Foo/)){ splice (@arrayAoH, $j, 1); } else { $j++; } } else { $i++;} }

    20050623 Edit by ysth: code tags

      here's the whole code segment
      I doubt that. This code won't parse.

      When using strict, you have to declare $arrayAoH first. Besides,

      $arrayAoH$j{$key} = $value;
      should read:
      $arrayAoH[$j]{$key} = $value;
      and
      if (($arrayAoH$j{"Model Number"} !~ m/^Foo/)){
      should be
      if (($arrayAoH[$j]{"Model Number"} !~ m/^Foo/)){
      Please post actual code...

      Paul

        Sorry. I added the definition of the array. The brackets around the array subscripts are there in the code; this web site's parser eats them. Thanks tl
      The eventual solution found by sapnac got buried below so I'll re-mention it here with an explanation. The issue is in the "initialization" code
      our @arrayAoH = "";
      The problem is that this doesn't just create an array. It creates an array with a single element that contains an empty string. It's basically equivalent to
      our @arrayAoH; $arrayAoH[0] = "";
      The solution is to either leave out the assignment or to assign it an empty list (which is different from an empty string):
      our @arrayAoH = ();
      More formally, a simple scalar value in list context is treated as a list of length one containing that value.