Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Array of hashes reference problem

by injunjoel (Priest)
on Jun 22, 2005 at 18:04 UTC ( [id://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.

-InjunJoel
"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?

    Thanks
    tl

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found