count on an array : it works but it may be simpler ?

hi,

My function works but i find that the line count=group.paper[1][1].count… is very big…no problem with vim ! but it must surely be a way to code it more simply or more aesthetic way.

more if I want to increase or decrease the number of rows or columns in my table I have to re-encode all
 

Thank you if you can make me discover a new trick

group.total=function()     count=group.paper[1][1].count+group.paper[2][1].count +group.paper[3][1].count+group.paper[4][1].count+group.paper[5][1].count+group.paper[1][2].count+group.paper[2][2].count+group.paper[3][2].count+group.paper[4][2].count+group.paper[5][2].count     if count == 10 then         print("ok")     else         print("false")     end end

[lua]

group.total = function()

      local count = 0

      for a = 1 , #group.paper, 1 do

            for b = 1 , #group.paper[a], 1 do

                  count = count + group.paper[a][b] .c ount

            end

      end

      if count == 10 then

        print(“ok”)

    else

        print(“false”)

    end

end

 

[/lua]

 

 

 

If you want to be even more performance efficient (depending on how large your data sets are) you could use local variables as well.

group.total = function() local count = 0 local group\_paper = group.paper for a = 1, #group\_paper do local paper\_a = group\_paper[a] for b = 1, #paper\_a do count = count + paper\_a[b].count end end if count == 10 then print("ok") else print("false") end end

thanks !!!

 (depending on how large your data sets are)

why did you say that ? what’s the limit for the local ?

if i use external module is the limit reach for one file or all the files combined ?

What he means is that if it’s possible the group.paper table could be say 100 x 100 or 1000 x 250 in size rather than 5 x 2 as it currently is, localising while looping through (i.e. local paper_a = group_paper[a]) might give a noticeable performance boost.

in other word i must localize always if it’s possible to have performance boost …  ?

Yes - I’m lazy and don’t usually bother unless it’s likely to have a noticeable impact.

If you are doing something simple 10 times you are highly unlikely to notice any difference in speed at all. But if it’s 100, 1,000, 10,000, those small boosts add up and might improve performance.

:slight_smile:

Exactly as Nick discriped it. :slight_smile:

hi,

I return with this example because I can not transpose what you had taught me earlier. how do you localise with this form…?

    local papier={}     local ext=".png"     for i = 1, 5 do         papier[i] = {};         for k = 1, 2 do             papier[i][k]="paper" .. (i-1)\*2+k .. ext             end     end

 

Not much to localize here, only one thing:

local papier = {} local ext = ".png" for i = 1, 5 do local page = {} papier[i] = page for k = 1, 2 do page[k] = "paper" .. (i-1)\*2+k .. ext end end

If the size of your second loop is constant you can optimize even more. But I think it isn’t worth the lossed flexibility.

local papier = {} local ext = ".png" for i = 1, 5 do local page = { "paper" .. (1-1)\*2+k .. ext, "paper" .. (2-1)\*2+k .. ext, } papier[i] = page end

[lua]

group.total = function()

      local count = 0

      for a = 1 , #group.paper, 1 do

            for b = 1 , #group.paper[a], 1 do

                  count = count + group.paper[a][b] .c ount

            end

      end

      if count == 10 then

        print(“ok”)

    else

        print(“false”)

    end

end

 

[/lua]

 

 

 

If you want to be even more performance efficient (depending on how large your data sets are) you could use local variables as well.

group.total = function() local count = 0 local group\_paper = group.paper for a = 1, #group\_paper do local paper\_a = group\_paper[a] for b = 1, #paper\_a do count = count + paper\_a[b].count end end if count == 10 then print("ok") else print("false") end end

thanks !!!

 (depending on how large your data sets are)

why did you say that ? what’s the limit for the local ?

if i use external module is the limit reach for one file or all the files combined ?

What he means is that if it’s possible the group.paper table could be say 100 x 100 or 1000 x 250 in size rather than 5 x 2 as it currently is, localising while looping through (i.e. local paper_a = group_paper[a]) might give a noticeable performance boost.

in other word i must localize always if it’s possible to have performance boost …  ?

Yes - I’m lazy and don’t usually bother unless it’s likely to have a noticeable impact.

If you are doing something simple 10 times you are highly unlikely to notice any difference in speed at all. But if it’s 100, 1,000, 10,000, those small boosts add up and might improve performance.

:slight_smile:

Exactly as Nick discriped it. :slight_smile:

hi,

I return with this example because I can not transpose what you had taught me earlier. how do you localise with this form…?

    local papier={}     local ext=".png"     for i = 1, 5 do         papier[i] = {};         for k = 1, 2 do             papier[i][k]="paper" .. (i-1)\*2+k .. ext             end     end