Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Array of hashes reference problem

by tlemons (Novice)
on Jun 22, 2005 at 17:25 UTC ( [id://469104]=perlquestion: print w/replies, xml ) Need Help??

tlemons has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm using the following code to process lines in the $discovered array, and create a number of hashed pairs in the same array row. The code works well.

until ($discovered[$i] eq ""){ ($key, $value) = split /:\s*/, $discovered[$i]; $arrayAoH[$j]{$key} = $value; $i++; }

But, when I enabled 'use strict' in my code, I get the following error when the line containing '$arrayAoH[$j]{$key} = $value;' is executed:

"Can't use string ("") as a HASH ref while "strict refs" in use at xxx.pl"

I've pored over the examples in 'Programming Perl', and have Google'd a bit. But I just don't understand why that line is wrong, and what I should do to fix it.

I tried using the following line in place of the offending line:

$arrayAoH[$j] = { $key => $value };

but it creates an array row with only a single has (whatever the last has processed was).

Thoughts, please!

Thanks tl

20050623 Edit by ysth: code tags

Replies are listed 'Best First'.
Re: Array of hashes reference problem
by tlm (Prior) on Jun 22, 2005 at 17:34 UTC

    Where is $j defined? What's it's value? It looks like $arrayAoH[$j] contains an empty string, not a hash ref.

    the lowliest monk

      Sorry, here's the whole code segment:

      our @arrayAoH = ""; 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/Item Number:/){ # 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++; } # If the selected item shouldn't be processed, remove this array row. if (($arrayAoH[$j]{"Model Number"} !~ m/^Foo/)){ splice (@arrayAoH, $j, 1); } else { $j++; } } else { $i++;} }

      20050623 Edit by ysth: code tags

        here's the whole code segment
        I doubt that. This code won't parse.

        When using strict, you have to declare $arrayAoH first. Besides,

        $arrayAoH$j{$key} = $value;
        should read:
        $arrayAoH[$j]{$key} = $value;
        and
        if (($arrayAoH$j{"Model Number"} !~ m/^Foo/)){
        should be
        if (($arrayAoH[$j]{"Model Number"} !~ m/^Foo/)){
        Please post actual code...

        Paul

        The eventual solution found by sapnac got buried below so I'll re-mention it here with an explanation. The issue is in the "initialization" code
        our @arrayAoH = "";
        The problem is that this doesn't just create an array. It creates an array with a single element that contains an empty string. It's basically equivalent to
        our @arrayAoH; $arrayAoH[0] = "";
        The solution is to either leave out the assignment or to assign it an empty list (which is different from an empty string):
        our @arrayAoH = ();
        More formally, a simple scalar value in list context is treated as a list of length one containing that value.
Re: Array of hashes reference problem
by injunjoel (Priest) on Jun 22, 2005 at 18:04 UTC
    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
      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>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found