Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi All, I wish to create array @

Status
Not open for further replies.

Johan De Graeve

Systems Engineer
Mar 16, 2018
1
BE
Hi All,

I wish to create array @c from arrays @a and @b

@a = (1, 2, 3)
@b = ("a", "b");

In @c I want all values of @a with all possible combinations of @b

@c = (
[ [1,"a"], [2,"a"], [3,"a"] ]
, [ [1,"a"], [2,"a"], [3,"b"] ]
, [ [1,"a"], [2,"b"], [3,"a"] ]
, [ [1,"b"], [2,"a"], [3,"a"] ]
, [ [1,"b"], [2,"b"], [3,"a"] ]
, [ [1,"b"], [2,"b"], [3,"b"] ]
)

What would we be the best way to accomplish this?

Thanks for helping out.

Johan
 
Johan De Graeve said:
I want all values of @a with all possible combinations of @b

What you want seems to be variations with repetition or so called n-tuples from a set of m-elements
In your case m = 2 and n = 3, so the number of all variations with repetition would be 2[sup]3[/sup] = 8, like this:
Code:
[
[[1, "a"], [2, "a"], [3, "a"]],
[[1, "a"], [2, "a"], [3, "b"]],
[[1, "a"], [2, "b"], [3, "a"]],
[[1, "a"], [2, "b"], [3, "b"]],
[[1, "b"], [2, "a"], [3, "a"]],
[[1, "b"], [2, "a"], [3, "b"]],
[[1, "b"], [2, "b"], [3, "a"]],
[[1, "b"], [2, "b"], [3, "b"]]
]
To generate these data I adapted the program from here
You can use it as template to write your own perl version.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top