Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Array of hashes reference problem

by tlemons (Novice)
on Jun 22, 2005 at 18:20 UTC ( [id://469118]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: Array of hashes reference problem
by sapnac (Beadle) on Jun 22, 2005 at 19:09 UTC
    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++;} }
        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!
        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found