-2

I need to use function cartesian from List::Gen. This function requires LIST as a second parameter, but I have an array, like

$VAR1 = [
 [
 1,
 2,
 3
 ],
 [
 'a',
 'b',
 'c'
 ],
 [
 'x'
 ]
 ];

How can I "convert" such array into list, so cartesian can be used? Putting array into list context does not seems to work in this case.

000
27.3k10 gold badges74 silver badges103 bronze badges
asked Jan 13, 2013 at 20:02
5
  • 2
    An array is a list, if you put it in list context. Commented Jan 13, 2013 at 20:04
  • 2
    The only time you need to differentiate between LIST and ARRAY is when you are using a function which handles arrays in a specific manner, such as push. Then the argument needs to be an array. There is no reversed incompatibility: A LIST can always be an array, but ARRAY cannot always be a list. Commented Jan 13, 2013 at 20:27
  • 1
    to summarize the comments so far: the example array you give shows a misunderstanding of perl syntax regarding parentheses vs. brackets. So we don't know what your actual array looks like. Show us, using your actual data and the output of use Data::Dumper; print Dumper \@a; Commented Jan 13, 2013 at 22:28
  • I am sorry for unclear question and example. I end up with the following solution: my @b = Set::CrossProduct->new([@a])->combinations; Commented Jan 14, 2013 at 0:28
  • If you want to ask a new a question, create a new post instead of replacing the old one! Commented Jan 14, 2013 at 21:09

2 Answers 2

5

Parens don't create lists, they just change precedence.

@a = ((1, 2, 3), (7, 8, 9));

is the same as

@a = (1, 2, 3, 7, 8, 9);

The examples of cartesian show a list of references to arrays

cartesian { $_[0] . $_[1] } [1,2,3], [7,8,9];

So it seems you want to create an array containing two elements, each a reference to another array.

@a = ( [1,2,3], [7,8,9] );

Then, to answer your question, evaluating @a in list context will return these two references.

cartesian { $_[0] . $_[1] } @a;
answered Jan 13, 2013 at 20:11
Sign up to request clarification or add additional context in comments.

3 Comments

So is your new question "How to convert an array of 6 elements into two reference to arrays of 3 elements?"? Not enough info. Not the right place to ask.
@Ωmega @a = ((1, 2, 3), (7, 8, 9)) is not an array of arrays. It's just an array. use Data::Dumper; print Dumper \@a; to see for yourself.
Please use Data::Dump or an equivalent to dump the contents of the array you want to pass to cartesian. If you publish the results here then we will be able to help you better.
0

In your new question, you have

$VAR1 = [
 [
 1,
 2,
 3
 ],
 [
 'a',
 'b',
 'c'
 ],
 [
 'x'
 ]
 ];

Despite your claims that it doesn't work, all you need is to evaluate the array in list context.

cartesian { ... } @$VAR1;

For example,

( cartesian { join '|', @_ } @$VAR1 )->say;

gives

1|a|x 1|b|x 1|c|x 2|a|x 2|b|x 2|c|x 3|a|x 3|b|x 3|c|x
answered Jan 14, 2013 at 21:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.