Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Array of hashes reference problem

by sapnac (Beadle)
on Jun 22, 2005 at 19:09 UTC ( #469135=note: print w/replies, xml ) Need Help??


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

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>

Replies are listed 'Best First'.
Re^4: Array of hashes reference problem
by tlemons (Novice) on Jun 22, 2005 at 19:38 UTC
    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!
        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

      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
Node Status?
node history
Node Type: note [id://469135]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-11-25 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?