array in hash

M

Me

How do I add element to array which is a hash element?
So far I have:
====================
push ($data{"ny"}{list}, { dat=>2});
push ($data{"ny"}{list}, { dat=>5});
push ($data{"dc"}{list}, { dat=>9});

====================

which generatees the following:
====================
Type of arg 1 to push must be array (not hash element) at hash.pl line
1, near "})"
Type of arg 1 to push must be array (not hash element) at hash.pl line
2, near "})"
Type of arg 1 to push must be array (not hash element) at hash.pl line
3, near "})"
=====================


I want to end up with something like:
data =>
{
location => "ny"
list = [
{dat=>2},
{dat=>5}
]
},
location => "dc"
list = [
{dat=>9},
]
},
 
D

David Squire

Me said:
How do I add element to array which is a hash element?

The important thing to remember here is that Perl lists and hashes are
inherently one dimensional. The trick used to make seemingly
multidimensional structures is to use references as the elements. See
perldoc perldsc.
So far I have:
====================
push ($data{"ny"}{list}, { dat=>2});
push ($data{"ny"}{list}, { dat=>5});
push ($data{"dc"}{list}, { dat=>9});

====================

which generatees the following:
====================
Type of arg 1 to push must be array (not hash element) at hash.pl line
1, near "})"
Type of arg 1 to push must be array (not hash element) at hash.pl line
2, near "})"
Type of arg 1 to push must be array (not hash element) at hash.pl line
3, near "})"
=====================

Yep. This is telling you exactly what the problem is. The first argument
to push must be an array, so you need to dereference the array reference
that is in the hash, viz:

----

#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper;

my %data = (
'ny' => {
list => [],
},
'dc' => {
list => [],
},
);

push (@{$data{'ny'}{list}}, {dat => 2});
push (@{$data{'ny'}{list}}, {dat => 5});
push (@{$data{'dc'}{list}}, {dat => 9});

print Dumper(\%data);

----

Output:


$VAR1 = {
'dc' => {
'list' => [
{
'dat' => 9
}
]
},
'ny' => {
'list' => [
{
'dat' => 2
},
{
'dat' => 5
}
]
}
};

I want to end up with something like:
data =>
{
location => "ny"
list = [
{dat=>2},
{dat=>5}
]
},
location => "dc"
list = [
{dat=>9},
]
},

This is not what your push statements said. Where was 'location' mentioned?


Regards,

DS
 
G

Gunnar Hjalmarsson

Me said:
How do I add element to array which is a hash element?
So far I have:
====================
push ($data{"ny"}{list}, { dat=>2});
push ($data{"ny"}{list}, { dat=>5});
push ($data{"dc"}{list}, { dat=>9});

Maybe you mean:

push @{ $data{"ny"}{list} }, { dat=>2 };

etc.
I want to end up with something like:
data =>
{
location => "ny"
list = [
{dat=>2},
{dat=>5}
]
},
location => "dc"
list = [
{dat=>9},
]
},

Well, that's another thing. Keep on trying, and use Data::Dumper to
study the resulting structure.
 
M

Me

Thanks.

Gunnar said:
Me said:
How do I add element to array which is a hash element?
So far I have:
====================
push ($data{"ny"}{list}, { dat=>2});
push ($data{"ny"}{list}, { dat=>5});
push ($data{"dc"}{list}, { dat=>9});


Maybe you mean:

push @{ $data{"ny"}{list} }, { dat=>2 };

etc.
I want to end up with something like:
data =>
{
location => "ny"
list = [
{dat=>2},
{dat=>5}
]
},
location => "dc"
list = [
{dat=>9},
]
},


Well, that's another thing. Keep on trying, and use Data::Dumper to
study the resulting structure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top