how to ForEach through ["name"] cells?

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

how to ForEach through ["name"] cells?

Post by Bugala »

I have table in way of:

Code: Select all

mytable = {
        ["cella"] = { itema = 1, itemb=2},
        ["cellb"] = { itema = 1, itemb=2],
       ...
              }
What I am trying is following:

Code: Select all

ForEach(mytable, Function (cell, data)
                                      data.itema=0
                            EndFunction)
By otherwords, I am trying to make every cells "itema" become 0.

But how do i crawl through every cell made this way?

wether i use cell.itema, or data.itema, neither works.

I also tried mytable[cell].itema, but that didnt work either.

So is there any way to access this through ForEach, or is there some other way to do this?
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: how to ForEach through ["name"] cells?

Post by Allanon »

Hello Bugala,

here it is using ForEach (your attempt seems correct to me):

Code: Select all

ForEach(mytable, Function(i, v) v.itema=0 EndFunction)
and using generic For

Code: Select all

For i, v In Pairs(mytable)
   v.itema = 0
Next
I've tried both methods and works :)
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: how to ForEach through ["name"] cells?

Post by Bugala »

@Allanon

Hmm. You are right. That piece of code does work right...

Have to figure out why does it sometimes work and sometimes it keeps complaining of " Wrong data types for concatenation!"


Another thing that I am wondering considering this.

That say I have a table:

Code: Select all

mytable = { x=1, y=2,
           ["cell1"] = {1, 2, 3},
           ["cell2"] = [4, 5, 6],
           ...
               }
Now is there any possiblity to use ForEach (or something similar) in such way, that it would only go through those ["cell1"], ["cell2"] subtables, and would simply skip x and y?

I know there is ForEachI-command, but that does exactly opposite of what I looking for.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: how to ForEach through ["name"] cells?

Post by Bugala »

Thanks to you pointing out that the example code worked, I was able to inspect more and was able to find solution.

When using ForEach command with FUNCTION (cell, data), before doing anything else, I first use GetType command to see that it is a table. If it is, then proceed to IF part of code.

This way I was able to go through only ["cella"], ["cellb"] kind of subtables, as long as i didnt have any other subtables there that werent supposed to be checked.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: how to ForEach through ["name"] cells?

Post by Allanon »

Great ;)
Post Reply