Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I make an array of hashes?

by vroom (His Eminence)
on Jan 12, 2000 at 00:34 UTC ( [id://1976]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (data structures)

How do I make an array of hashes?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I make an array of hashes?
by chromatic (Archbishop) on Mar 31, 2000 at 00:42 UTC
    my @array = ( { 'name' => 'Chuck', 'job' => 'roustabout', }, { 'name' => 'CowboyNeal', 'job' => 'code monkey', }, );
    You can also use normal array operations (such as push) to modify an array:
    push @array, { 'name' => 'vroom', 'job' => 'stacking blocks' }; print $array[0]->{name};
    See perlman:perldsc for more information.
Re: How do I make an array of hashes?
by fx (Pilgrim) on Dec 19, 2003 at 23:26 UTC

    If the hash has already been created, you can store references to the hash in arrays:

    push(@array, \%some_hash);

    and then when you pop them off:

    $hash_ref = pop(@array);

    you'll get a scalar back which can be dereferenced to get at the values of the hash:

    $value = $hash_ref->{'key'};

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1976]
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: (5)
As of 2024-04-18 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found