joining arrays inside hash

Discussion in 'Perl Misc' started by justme, Apr 9, 2005.

  1. justme

    justme Guest

    hi

    i have a hash with a few arrays as elements

    eg

    %hash = ( 1 => ("A", "B"),
    2 => ("C", "D"),
    3 => ("E","F"),
    4 => ("G","H"),
    5 => ("1","2"),
    6 => ("one","two");
    }

    These array elements are longer strings, but i just put it here for
    short as ABCD....
    I wish to join up the first 4 array elements such that its output is
    like this
    AE, AF, BE,BF, CG , CH , DG,DH. ( key 1 and 3 will join, key 2 and 4
    will join)

    Then after that, the result will join with elements of key 5 and 6
    (AE,AF,"1","one") , (BE,BF,"1","one"),
    (AE,AF,"2","two"),(BE,BF,"2","two"), and so on ...
    Can someone suggest a good algorithm to do this?

    thanks..
    justme, Apr 9, 2005
    #1
    1. Advertising

  2. justme

    Guest

    justme wrote:

    > i have a hash with a few arrays as elements


    No you don't. The elements of a hash are scalars. A scalar can be an
    array _reference_.

    > %hash = ( 1 => ("A", "B"),
    > 2 => ("C", "D"),
    > 3 => ("E","F"),
    > 4 => ("G","H"),
    > 5 => ("1","2"),
    > 6 => ("one","two");
    > }


    That is

    %hash = ( 1 => "A",
    B => 2,
    C => "D",
    3 => "E",
    F => 4,
    # etc...

    You mean

    %hash = ( 1 => ["A", "B"],
    2 => ["C", "D"],
    3 => ["E","F"],
    4 => ["G","H"],
    5 => ["1","2"],
    6 => ["one","two"],
    );


    > These array elements are longer strings, but i just put it here for
    > short as ABCD....
    > I wish to join up the first 4 array elements such that its output is
    > like this
    > AE, AF, BE,BF, CG , CH , DG,DH. ( key 1 and 3 will join, key 2 and 4
    > will join)
    >
    > Then after that, the result will join with elements of key 5 and 6
    > (AE,AF,"1","one") , (BE,BF,"1","one"),
    > (AE,AF,"2","two"),(BE,BF,"2","two"), and so on ...
    > Can someone suggest a good algorithm to do this?


    Not. There doesn't seem much of a to be a pattern to what you want do
    all you so I all I can suggest is just do it. To come up with an
    algorithm would imply deciding how it would extend.
    , Apr 9, 2005
    #2
    1. Advertising

  3. justme

    Guest

    justme <> wrote:
    > i have a hash with a few arrays as elements


    > eg


    > %hash = ( 1 => ("A", "B"),
    > 2 => ("C", "D"),
    > 3 => ("E","F"),
    > 4 => ("G","H"),
    > 5 => ("1","2"),
    > 6 => ("one","two");
    > }


    No you don't. You would, if the syntax were correct, have a hash whose
    keys would be 1, "B", "C", 3, "F"...

    Did you perhaps mean anonymous arrays as in

    %hash = ( 1 => [ "A", "B"], ... );

    > These array elements are longer strings, but i just put it here for
    > short as ABCD....
    > I wish to join up the first 4 array elements such that its output is
    > like this
    > AE, AF, BE,BF, CG , CH , DG,DH. ( key 1 and 3 will join, key 2 and 4
    > will join)


    > Then after that, the result will join with elements of key 5 and 6
    > (AE,AF,"1","one") , (BE,BF,"1","one"),
    > (AE,AF,"2","two"),(BE,BF,"2","two"), and so on ...
    > Can someone suggest a good algorithm to do this?


    Just loop through the data. For example the following loops will create
    the AE, AF, BE, ... sequence.

    for my $i ( 1..2 ) {
    for my $j ( 0..1 ) {
    for my $k ( 0..1 ) {
    print $hash{$i}->[$j] . $hash{$i + 2}->[$k];
    }
    }
    }

    You will need to save the values created instead of printing
    them and then run another loop to create the final results desired.

    Axel
    , Apr 9, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  3. Adam Akhtar
    Replies:
    5
    Views:
    635
    Adam Akhtar
    Mar 25, 2008
  4. Dan

    INNER JOINING Two Arrays

    Dan, Aug 24, 2009, in forum: Ruby
    Replies:
    3
    Views:
    94
    Giampiero Zanchi
    Aug 25, 2009
  5. Edward Wijaya

    Joining 2 arrays into hashes

    Edward Wijaya, Jun 1, 2004, in forum: Perl Misc
    Replies:
    20
    Views:
    228
    Ben Morrow
    Jun 4, 2004
Loading...

Share This Page