×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Hi All, I wish to create array @

Hi All, I wish to create array @

Hi All, I wish to create array @

(OP)
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

RE: Hi All, I wish to create array @

What have you tried so far?

RE: Hi All, I wish to create array @

Quote (Johan De Graeve)


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
https://en.wikipedia.org/wiki/Tuple#n-tuples_of_m-...
In your case m = 2 and n = 3, so the number of all variations with repetition would be 23 = 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
https://stackoverflow.com/questions/2366074/code-f...
You can use it as template to write your own perl version.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close