Professional Web Applications Themes

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. #1

    Default 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. #2

    Default 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. #3

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

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

    Sorry this should read:

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

  4. #4

    Default 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. #5

    Default 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. #6

    Default 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. #7

    Default 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

    #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 ~/
    Jan Guest

  8. #8

    Default 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. #9

    Default 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. #10

    Default 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.
    I will follow your suggestion.

    Regards,
    Edward WIJAYA
    Edward Guest

  11. #11

    Default 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

Similar Threads

  1. Sorting an array of hashes
    By Chris in forum PERL Beginners
    Replies: 7
    Last Post: August 6th, 02:29 PM
  2. Help with array, hashes and objects
    By George in forum PERL Beginners
    Replies: 2
    Last Post: February 18th, 11:29 AM
  3. Printing Array of Hashes
    By William Martell in forum PERL Beginners
    Replies: 3
    Last Post: December 23rd, 10:24 PM
  4. Slice an array of hashes?
    By Graham in forum PERL Miscellaneous
    Replies: 6
    Last Post: September 11th, 07:20 PM
  5. adding to an array of hashes
    By Tom in forum PERL Miscellaneous
    Replies: 6
    Last Post: August 31st, 08:53 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139