Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:27:14+00:00 2026-06-13T08:27:14+00:00

I’m trying to execute a function with every single combination of elements from a

  • 0

I’m trying to execute a function with every single combination of elements from a table. (In Lua). The table and the elements can change, but the structure will stay the same. The table is organized so that [1] of it would be the first argument of the function, and so on and so on.

If this is a table that I’ve got,

Table = {
    [1] = {Player1, Player2}
    [2] = {PlayerA, PlayerB, PlayerC}
    [3] = {PlayerOne, PlayerTwo}
}

If I wrote that out manually, it would probably look like this: (Given that the function is named Exe).

Exe(Player1, PlayerA, PlayerOne)
Exe(Player2, PlayerA, PlayerOne)
Exe(Player3, PlayerA, PlayerOne)

Exe(Player1, PlayerB, PlayerOne)
Exe(Player2, PlayerB, PlayerOne)
Exe(Player3, PlayerB, PlayerOne)

Exe(Player1, PlayerC, PlayerOne)
Exe(Player2, PlayerC, PlayerOne)
Exe(Player3, PlayerC, PlayerOne)


Exe(Player1, PlayerA, PlayerTwo)
Exe(Player2, PlayerA, PlayerTwo)
Exe(Player3, PlayerA, PlayerTwo)

Exe(Player1, PlayerB, PlayerTwo)
Exe(Player2, PlayerB, PlayerTwo)
Exe(Player3, PlayerB, PlayerTwo)

Exe(Player1, PlayerC, PlayerTwo)
Exe(Player2, PlayerC, PlayerTwo)
Exe(Player3, PlayerC, PlayerTwo)

However, I don’t WANT to write that out, and it breaks my general rule of thumb that if you’re copying and pasting in a program, you’re doing it wrong.

So instead, I would like to go through the table and execute every single possible combination. The problem so that the table can (potentially) have any number of tables inside of it, and also that the table inside of the table can potentially have unlimited number of values.

For example, the table could end up looking like this:

Table = {
    [1] = {Player1, Player2}
    [2] = {PlayerA}
    [3] = {PlayerOne}
}

In which execution would end up looking like this manually:

Exe(Player1, PlayerA, PlayerOne)
Exe(Player2, PlayerA, PlayerOne)

Also, the table might end up like this:

Table = {
    [1] = {Player1, Player2}
    [2] = {PlayerA}
    [3] = {PlayerOne}
    [4] = {PlayerUno, PlayerDos}
    [5] = {PlayerApple, PlayerBoy, PlayerCat, PlayerDog}
}

In which the exeuction would end up like..

Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerApple)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerApple)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerApple)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerApple)


Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerBoy)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerBoy)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerBoy)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerBoy)


Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerCat)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerCat)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerCat)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerCat)


Exe(Player1, PlayerA, PlayerOne, PlayerUno, PlayerDog)
Exe(Player2, PlayerA, PlayerOne, PlayerUno, PlayerDog)

Exe(Player1, PlayerA, PlayerOne, PlayerDos, PlayerDog)
Exe(Player2, PlayerA, PlayerOne, PlayerDos, PlayerDog)

As you can see, I have found a pattern… I was able to divide the above ‘Execution’ thing above into segments/groups, such as line 1 and line 2 have one change. Then, they get copied into line 4 and 5, but the next variable get’s changed.

As you can see, I’m having trouble putting that pattern into code. I think some function recursion will be required, but I’m not sure how to pull it off or recurse through it. I’m thinking that I’ll have to use functions with … as the arguments, and the unpack function, but I’m not sure how this would even work.

Also, the reason this is required, and not just manually copying and pasting it (Which actually would be easier), is because the content of the table will be generated.

Can you guys help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:27:15+00:00Added an answer on June 13, 2026 at 8:27 am

    Use recursion.

    Imagine a function map_all (fcn, tab, idx, ...) that maps fcn to the product of the elements of all the tables tab[1] to tab[idx] prepended to ...

    The base case is when idx is less than 1. In that case, simply apply fcn(...)

    Otherwise, map_all(fcn, tab, idx-1, <el>, ...) for all <el> in tab[idx]

    function map_all (fcn, tab, idx, ...)
        if idx < 1 then
            fcn(...)
        else
            local t = tab[idx]
            for i = 1, #t do map_all(fcn, tab, idx-1, t[i], ...) end
        end
    end
    

    So,

    > Table = {
    >>     [1] = {'Player1', 'Player2'},
    >>     [2] = {'PlayerA', 'PlayerB', 'PlayerC'},
    >>     [3] = {'PlayerOne', 'PlayerTwo'}
    >> }
    > map_all(print, Table, #Table)
    Player1 PlayerA PlayerOne
    Player2 PlayerA PlayerOne
    Player1 PlayerB PlayerOne
    Player2 PlayerB PlayerOne
    Player1 PlayerC PlayerOne
    Player2 PlayerC PlayerOne
    Player1 PlayerA PlayerTwo
    Player2 PlayerA PlayerTwo
    Player1 PlayerB PlayerTwo
    Player2 PlayerB PlayerTwo
    Player1 PlayerC PlayerTwo
    Player2 PlayerC PlayerTwo
    

    and

    > Table = {
    >>     [1] = {'Player1', 'Player2'},
    >>     [2] = {'PlayerA'},
    >>     [3] = {'PlayerOne'}
    >> }
    > map_all(print, Table, #Table)
    Player1 PlayerA PlayerOne
    Player2 PlayerA PlayerOne
    

    and

    > Table = {
    >>     [1] = {'Player1', 'Player2'},
    >>     [2] = {'PlayerA'},
    >>     [3] = {'PlayerOne'},
    >>     [4] = {'PlayerUno', 'PlayerDos'},
    >>     [5] = {'PlayerApple', 'PlayerBoy', 'PlayerCat', 'PlayerDog'},
    >> }
    > map_all(print, Table, #Table)
    Player1 PlayerA PlayerOne   PlayerUno   PlayerApple
    Player2 PlayerA PlayerOne   PlayerUno   PlayerApple
    Player1 PlayerA PlayerOne   PlayerDos   PlayerApple
    Player2 PlayerA PlayerOne   PlayerDos   PlayerApple
    Player1 PlayerA PlayerOne   PlayerUno   PlayerBoy
    Player2 PlayerA PlayerOne   PlayerUno   PlayerBoy
    Player1 PlayerA PlayerOne   PlayerDos   PlayerBoy
    Player2 PlayerA PlayerOne   PlayerDos   PlayerBoy
    Player1 PlayerA PlayerOne   PlayerUno   PlayerCat
    Player2 PlayerA PlayerOne   PlayerUno   PlayerCat
    Player1 PlayerA PlayerOne   PlayerDos   PlayerCat
    Player2 PlayerA PlayerOne   PlayerDos   PlayerCat
    Player1 PlayerA PlayerOne   PlayerUno   PlayerDog
    Player2 PlayerA PlayerOne   PlayerUno   PlayerDog
    Player1 PlayerA PlayerOne   PlayerDos   PlayerDog
    Player2 PlayerA PlayerOne   PlayerDos   PlayerDog
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.