in reply to Re^3: Array of hashes reference problem
in thread Array of hashes reference problem

Thanks for the help. Okay. This time, I'm SURE I've got all the pieces, because I ran this as a separate Perl program. And, I get the following error when I run it:

Can't use string ("") as a HASH ref while "strict refs" in use at test-hash.pl line 19

Line 19 contains $arrayAoH$j{$key} = $value;

Thoughts?

Thanks! tl

#!/usr/bin/perl -w use strict; my @arrayAoH = ""; my $out = "First one: one Second one: second"; my @discovered = split /\n/, $out; 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/First one:/){ # 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++; } $j++; } else { $i++;} }

Replies are listed 'Best First'.
Re^5: Array of hashes reference problem
by sapnac (Beadle) on Jun 22, 2005 at 20:32 UTC
    Check this code; The code posted goes in an infinite loop.
    #!/usr/bin/perl -w use strict; my @arrayAoH; my $out = "First one: one Second one: second"; my ( $i,$j ); my ($key,$value); my @discovered = split /\n/, $out; $i = 0; # i holds the line number of the input (@discovered) array. $j = 0; # j holds the number of the table row. until ($i > $#discovered){ ($key, $value) = split /:\s*/, $discovered[$i]; $arrayAoH[$j]{$key} = $value; $i++; }
    Hope it helps! Good Luck!
      Thanks very much! It solved my problem. Aside from the infinite loop, your code worked and my didn't. I stared at it, and noticed that the important difference was:

      Your code
      my @arrayAoH;

      My code
      my @arrayAoH = "";

      By initializing the array, I shot myself in the foot. When I removed that "" initialization, my code worked great.

      Thanks VERY much for the help!

      tl

Re^5: Array of hashes reference problem
by djohnston (Monk) on Jun 22, 2005 at 20:29 UTC
    You could try ($arrayAoH[$j]||={})->{$key} = $value; which should get you past that warning, but I suspect you will encounter other issues. I also suspect that there might be a much simpler algorithm to get done whatever it is you're attempting to do. Perhaps it'd be best if you post more sample data along with a detailed description of the data structure you're trying to build out of it.