perl howto push a hash onto an array

This could also be called how to make an arrays of hashes. The basic idea is to create a list of items that when iterated, returns individual hashes.

use Data::Dumper;
my @array;
push @array, {'key1' => 'value1', 'key2' => 'value2'};
push @array, {'key1' => 'value1', 'key2' => 'value2'};
push @array, {'key1' => 'value1', 'key2' => 'value2'};
print Dumper(@array);

Will give you:

$VAR1 = {
'key2' => 'value2',
'key1' => 'value1'
};
$VAR2 = {
'key2' => 'value2',
'key1' => 'value1'
};
$VAR3 = {
'key2' => 'value2',
'key1' => 'value1'
};

Snazzy!

Did you find this post useful or have questions or comments?  Please let me know!

This entry was posted in How Tos, perl, Programming. Bookmark the permalink.

2 Responses to perl howto push a hash onto an array

  1. Pingback: mikerochford.com » Howto push a hash onto an array

  2. Pingback: Python HOWTO Push a Dict on to a List (push a hash on to an array) | Brain Goo

Leave a Reply

Your email address will not be published. Required fields are marked *