joining arrays inside hash

J

justme

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

nobull

justme said:
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.
 
A

axel

justme said:
i have a hash with a few arrays as elements

%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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top