Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Array of hashes reference problem

by injunjoel (Priest)
on Jun 22, 2005 at 18:04 UTC ( #469114=note: print w/replies, xml ) Need Help??

in reply to Array of hashes reference problem

Greetings all,
Aside from suggesting Data::Dumper or Dumpvalue for complex data-structure investigations I think a read up on perldsc would be worthwhile.
However my short answer is to make sure that $key contains something prior to the assignment.

"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Replies are listed 'Best First'.
Re^2: Array of hashes reference problem
by tlemons (Novice) on Jun 22, 2005 at 18:20 UTC
    Thank you for your reply. I'll re-read the DSC.

    I just stepped through the code with Komodo, and I'm absolutely sure that both $key and $value have values in them. Does it matter that $key contains a string with a space in the middle (like 'Foo Bar')? Any other suggestions?


      hi! I ran the following code and it executed without error;
      Please check the input.
      did not check the result but did not get error when executed;
      use strict; my $i = 0; <br> my $j = 0; <br> my @discovered;<br> my @arrayAoH;<br> <br> @discovered =(" : hfjfda","rk qjhre:kjfsngfnfd","sdfjlksjf : sfljkhegl +erg"); <br> <br> my $key;<br> my $value;<br> until ($i eq $#discovered){<br> if ($discovered[$i] =~ m/Item Number:/){<br> until ($discovered[$i] eq ""){<br> ($key, $value) = split /:\s*/, $discovered[$i];<br> $arrayAoH[$j]{$key} = $value;<br> $i++;<br> }<br> if (($arrayAoH[$j]{"Model Number"} !~ m/^Foo/)){<br> splice (@arrayAoH, $j, 1);<br> }<br> else {<br> $j++;<br> }<br> }<br> else {<br> $i++;}<br> }<br>
        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 line 19

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


        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++;} }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://469114]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2019-07-21 13:08 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (8 votes). Check out past polls.