How to accumulate Hashes of Array value with the same key?

2004-09-30 Thread Edward Wijaya
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 accumulating the value of HoA2,HoA3 into  HoA.
Thanks very much for the time.
Regards,
Edward WIJAYA
SINGAPORE
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: How to accumulate Hashes of Array value with the same key?

2004-09-30 Thread Shaw, Matthew
> 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]);
> 

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

> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
> 


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: How to accumulate Hashes of Array value with the same key?

2004-09-30 Thread Shaw, Matthew

> > 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]);
> >
> 
> push @{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA2{key1}});
> 

Sorry this should read:

@{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA3{key1}});

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to accumulate Hashes of Array value with the same key?

2004-09-30 Thread Dave Gray
> 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]);

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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Edward Wijaya
On Thu, 30 Sep 2004 17:03:40 -0400, Dave Gray <[EMAIL PROTECTED]> wrote:
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}?
As you mention above. That is exactly what I mean:
This sounds like it's part of a bigger question. Can you give us some
more background info?
What I am trying to do is to accumulate, every new HoA generated
into existing HoA, and join the values if they have the same key.
Dave

Regards,
Edward WIJAYA
SINGAPORE
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Edward Wijaya
On Thu, 30 Sep 2004 14:23:47 -0300, Shaw, Matthew <[EMAIL PROTECTED]>
wrote:
Thanks Matt,

> my %HoA = (key1 => ['A',1]);
> my %HoA2 = (key1 => ['B',2]);
> my %HoA3 = (key1 => ['C',2]);
>
> into:
>
Only this one works
push @{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA2{key1}});

Not this
Sorry this should read:
@{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA3{key1}});
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
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Jan Eden
Edward Wijaya wrote on 01.10.2004:

>On Thu, 30 Sep 2004 14:23:47 -0300, Shaw, Matthew <[EMAIL PROTECTED]>
>wrote:
>
>Thanks Matt,
>
>
>>> > my %HoA = (key1 => ['A',1]);
>>> > my %HoA2 = (key1 => ['B',2]);
>>> > my %HoA3 = (key1 => ['C',2]);
>>> >
>>> > into:
>>> >
>
>Only this one works
>
>>> push @{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA2{key1}});
>
>
>Not this
>> Sorry this should read:
>> @{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA3{key1}});
>
>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]};

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

So you could do this

#get the actual length of your target array and add one
$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 ~/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Jan Eden
Edward Wijaya wrote on 01.10.2004:

>On Thu, 30 Sep 2004 14:23:47 -0300, Shaw, Matthew <[EMAIL PROTECTED]>
>wrote:
>
>Thanks Matt,
>
>
>>> > my %HoA = (key1 => ['A',1]);
>>> > my %HoA2 = (key1 => ['B',2]);
>>> > my %HoA3 = (key1 => ['C',2]);
>>> >
>>> > into:
>>> >
>
>Only this one works
>
>>> push @{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA2{key1}});
>
It is valid Perl code, but it should not have the desired effect. Note the double 
reference to HoA2, corrected below.
>
>Not this
>> Sorry this should read:
>> @{$HoA{key1}}, ( @{$HoA2{key1}}, @{$HoA3{key1}});

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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Dave Gray
> $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.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Dave Gray
On Fri, 1 Oct 2004 10:51:50 -0400, Dave Gray <[EMAIL PROTECTED]> wrote:
> > $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]
> };

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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to accumulate Hashes of Array value with the same key?

2004-10-01 Thread Edward Wijaya
On Fri, 1 Oct 2004 10:51:50 -0400, Dave Gray <[EMAIL PROTECTED]> wrote:
$HoA = ( key1 => [
  ['A',1], ['B',2], ['C',3]
]);
Which is clunky when you want to detect duplicates, which is why I
suggested the hash.

I think you are right Dave.
I will follow your suggestion.
Regards,
Edward WIJAYA
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]