0

i have two set of dynamic arrays that have some elements like this:

 my @arr1 = qw( e1 e2 );
 my @arr2 = qw( n1 n2 );

i want to create pairs such as (e1,n1), (e1,n2), (e2,n1) and (e2,n2) or

 my @arr1 = qw( e1 );
 my @arr2 = qw( n1 n2 );

to get (e1,n1), (e1,n2)

but i am not sure how to create this and store it in hash for processing.

Can any one help?

2
  • What would you want your keys to be if this was a hash? Would an array of array references make sense? ([e1,n1], [e1,n2]) Commented Jul 6, 2014 at 15:27
  • Re "store it in hash for processing", What exactly do you want the hash to look like??? Commented Jul 6, 2014 at 15:45

1 Answer 1

2
my @arr1 = qw( e1 e2 );
my @arr2 = qw( n1 n2 );

for my $x (@arr1) {
   for my $y (@arr2) {
      print("($x,$y)\n");
   }
}

If you had an arbitrary number of arrays:

use Algorithm::Loops qw( NestedLoop );

my @arrs = (
   [qw( e1 e2 )],
   [qw( n1 n2 )],
);

my $iter = NestedLoops(\@arrs);

while (my @pick = $iter->()) {
   print("(" . join(',', @pick) . ")\n");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.