# How to aculate Hashes of Array value with the same key? - PERL Beginners

Hi, I have thre HoAs with the same key but different value. How can I efficiently join the HoA: my %HoA = (key1 => ['A',1]); my %HoA2 = (key1 => ['B',2]); my %HoA3 = (key1 => ['C',2]); into: %HoA = (key1 => ['A',1],['B',2],['C',2]); namely aculating the value of HoA2,HoA3 into HoA. Thanks very much for the time. Regards, Edward WIJAYA SINGAPORE...

1. ## How to aculate Hashes of Array value with the same key?

Hi,

I have thre HoAs with the same key but different value.
How can I efficiently join the HoA:

my %HoA = (key1 => ['A',1]);
my %HoA2 = (key1 => ['B',2]);
my %HoA3 = (key1 => ['C',2]);

into:

%HoA = (key1 => ['A',1],['B',2],['C',2]);

namely aculating the value of HoA2,HoA3 into HoA.

Thanks very much for the time.

Regards,
Edward WIJAYA
SINGAPORE
Edward Guest

2. ## RE: How to aculate Hashes of Array value with the same key?

> I have thre HoAs with the same key but different value.

push {\$HoA{key1}}, ( {\$HoA2{key1}}, {\$HoA2{key1}});

Matthew Guest

3. ## RE: How to aculate Hashes of Array value with the same key?

>
> push {\$HoA{key1}}, ( {\$HoA2{key1}}, {\$HoA2{key1}});
> [/ref]

{\$HoA{key1}}, ( {\$HoA2{key1}}, {\$HoA3{key1}});
Matthew Guest

4. ## Re: How to aculate Hashes of Array value with the same key?

> I have thre HoAs with the same key but different value.

I'm not sure what you want to do here... do you want to combine all
the values into one array reference, stored in \$HoA{key1}? Or do you
want \$HoA{key1} to be an array of arrays?

Another solution might be to use a hash of hashes:

%HoA = (
key1 => {
A => 1,
B => 2,
C => 3
}
);

This sounds like it's part of a bigger question. Can you give us some
more background info?

Dave
Dave Guest

5. ## Re: How to aculate Hashes of Array value with the same key?

On Thu, 30 Sep 2004 17:03:40 -0400, Dave Gray <com> wrote:

As you mention above. That is exactly what I mean:

What I am trying to do is to aculate, every new HoA generated
into existing HoA, and join the values if they have the same key.

Regards,
Edward WIJAYA
SINGAPORE
Edward Guest

6. ## Re: How to aculate Hashes of Array value with the same key?

On Thu, 30 Sep 2004 14:23:47 -0300, Shaw, Matthew <com>
wrote:

Thanks Matt,

[/ref][/ref]

Only this one works
[/ref]

Not this

However what it gives is that it create one single array,
and not preserving the array that group ['A',1] etc, like before
namely:

print Dumper \%HoA;
\$VAR1 = {
'key1' => [
'A',
1,
'B',
2,
'C',
2
]
};

not;

\$VAR1 = { 'key1' => ['A',1],['B',2],['C',2]};

Regards,
Edward WIJAYA
SINGAPORE
Edward Guest

7. ## Re: How to aculate Hashes of Array value with the same key?

Edward Wijaya wrote on 01.10.2004:
[/ref]
>
>Only this one works
> [/ref]
>
>
>Not this
>
>However what it gives is that it create one single array,
>and not preserving the array that group ['A',1] etc, like before
>namely:
>
>print Dumper \%HoA;
>\$VAR1 = {
> 'key1' => [
> 'A',
> 1,
> 'B',
> 2,
> 'C',
> 2
> ]
> };
>
>not;
>
>\$VAR1 = { 'key1' => ['A',1],['B',2],['C',2]};[/ref]

So you want a hash of an array of arrays, right? This is adding another level of encapsulation.

So you could do this

\$array_element = \$#HoA{key1}++;
#push the new list of values into a new array inside the HoA
push \$HoA{key1}->[\$array_element], {\$HoA2{key1}}

Or did I get you wrong?

- Jan
--
There's no place like ~/
Jan Guest

8. ## Re: How to aculate Hashes of Array value with the same key?

Edward Wijaya wrote on 01.10.2004:
[/ref]
>
>Only this one works
> [/ref]
>[/ref]
It is valid Perl code, but it should not have the desired effect. Note the double reference to HoA2, corrected below. [/ref]

- Jan
--
There are 10 kinds of people: those who understand binary, and those who don't
Jan Guest

9. ## Re: How to aculate Hashes of Array value with the same key?

> \$VAR1 = { 'key1' => ['A',1],['B',2],['C',2]};

That isn't going to do what you think it is... What you're asking for
there is to use the ['B', 2] array reference as a hash key...

\$VAR1 = {
'ARRAY(0x804ca54)' => ['C',2],
'key1' => ['A',1]
};

In order to get close to what I think you're trying to describe, you'd
have to have another array reference and stuff those three arrays in
there.

\$HoA = ( key1 => [
['A',1], ['B',2], ['C',3]
]);

Which is clunky when you want to detect duplicates, which is why I
suggested the hash.
Dave Guest

10. ## Re: How to aculate Hashes of Array value with the same key?

On Fri, 1 Oct 2004 10:51:50 -0400, Dave Gray <com> wrote:

I think you are right Dave.

Regards,
Edward WIJAYA
Edward Guest

11. ## Re: How to aculate Hashes of Array value with the same key?

On Fri, 1 Oct 2004 10:51:50 -0400, Dave Gray <com> wrote:
>
> That isn't going to do what you think it is... What you're asking for
> there is to use the ['B', 2] array reference as a hash key...
>
> \$VAR1 = {
> 'ARRAY(0x804ca54)' => ['C',2],
> 'key1' => ['A',1]
> };[/ref]

Actually, the string representation of the ['B',2] is what gets used,
so the actual ['B',2] array gets thrown away.
Dave Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•