# joining arrays inside hash

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

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

## Re: joining arrays inside hash

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.

## Re: joining arrays inside hash

> 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->[\$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