can foreach command include tables name it is going through?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

can foreach command include tables name it is going through?

Post by Bugala »

I already solved my problem alternative way as i came to conclusion it was better to be done different way, but I am anyway wondering for future purposes if following would be possible.

I had several different card deck tables:

abilitycards
blesingcards
recoursecards

These tables contained a list of cards and wether they were in use or not, as example:

Code: Select all

abilitycards = {
            ["farmer"] = {inuse=TRUE}
                    }
Idea was that before starting the game, player could choose to possibly not include some of the cards in game, by changing that cards "inuse" variable to FALSE.

Hence at beginning of game, it would go through all the cards and check if they have "inuse" set to TRUE or not, and if TRUE, then add it to the actual playing deck.


What I wanted to do was something like this:

Code: Select all

function FormDeck(deckname)
foreach(deckname, checktoaddcard)
endfunction

function checktoaddcard(card, data)
   if data.inuse=true then insertItem(???deckname???, card)
endfunction


FormDeck("abilitycards")
FormDeck("blessingcards")
FormDeck("resourcecards")
The obvious problem was on how do i pass this "decksname" variable to this checktoaddcard function to form a new deck that would contain those specifc type of cards?

And yes, I know just putting the name that was given when entering the function is not enough yet, since it will be replacing the original deck with new one making original one destroyed, but I used this wrong version to explain the main problem, for as long as i could first somehow pass the "deckname" through, its not hard to get it form its own deck of cards with different name.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: can foreach command include tables name it is going thro

Post by Bugala »

And also since on that topic already:
http://www.hollywood-mal.com/docs/html/ ... rEach.html

I think you should add example of using:

Code: Select all

foreach(table, func)

function func(index, value)
debugprint("index:"..index.." value:"..value)
endfunction

for the current example is quite useless actually unless you have some knowledge about foreach command from some other programming language first, since with the description and shown example, it seems like all you can do with it, is to print out all the stuff in your table, but not much else.

I mean, the description is okay, as it tells there are coming two arguments. But what two arguments means is something bit hard for others but advanced programmers to realise.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: can foreach command include tables name it is going thro

Post by airsoftsoftwair »

I think what you'll want to use to solve this problem is the generic for statement using Pairs() as the iterator function. See here:
http://www.hollywood-mal.com/docs/html/ ... gFor_.html

Concerning the example in the documentation: I think it's simple and straight forward :)
Post Reply