Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Getting information from an array of hashes

by AgentM (Curate)
on Apr 25, 2001 at 20:38 UTC ( #75529=note: print w/ replies, xml ) Need Help??


in reply to Getting information from an array of hashes

When you assign to hash like that, Perl is expecting to see an even-number-sized list of elements to put in the hash- key/ value pairs. If $diary_data[0] is a reference (it is) to the array where the key/value pairs are (this I don't know), then you'll want to use

@{$diary_data[0]};
This resolves the pointer/reference and "enters" the array into the hash as key/value pairs. Then your print statements will work fine. Also, be sure to use strict;, use warnings;, and use diagnostics; for better error-reporting, though I'm not sure if you've already throw those in.

Update: Ovid's node is probably right, now that I reread your statement "returns array of hashes".

Update2: AgentM wins! Fatality!

AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.


Comment on Re: Getting information from an array of hashes
Select or Download Code
Re: Re: Getting information from an array of hashes
by chumley (Sexton) on Apr 25, 2001 at 23:38 UTC

    If I put in a line that says

    print "$diary_data[0]\n";

    it prints:

    ARRAY(0x3bfda0)

    If I change the print slightly, like this:

    print "@{$diary_data[0]}\n";

    This is what I get:

    HASH(0x3bfdc4)

    Then if I try

    print "%{$diary_data[0]}\n";

    it gives me:

    %{ARRAY(0x3bfdd0)}

    I don't think I'm understanding this correctly. The first example looks to me like a scalar reference, pulling data out of an array; but since the array contains hashes, then how does this get pulled out? Is this "ARRAY(0x3bfda0)" line an internal reference to how Perl remembers something?

    Anyway, I'm off to read perldsc for a while.

    Chumley
      Try Data::Dumper. Thanks for posting this since this helps us out alot in disagnosing the problem. What you don't want to do is print references, since they're rarely helpful, but this case is an exception. We are shown that you probably have an array of arrays which you should be assigning to a hash, so my above suggestion for an assignment would be best.

      You're right. What you see being printed is an internal representation of Perl's memory management, which isn't very helpful to you- so use Data::Dumper to get the right debugging info. I think we even have a Tutorial here about it. Good luck!

      AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-08-30 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls