lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi Philipe,

Thank you very much. This is very relevant. My JSON is ordered and then month actually comes as "mm" only but I think I will implement your suggestion to improve the code resiliency to changes in the JSON feed.



Le ven. 12 juin 2020 à 17:57, Philippe Verdy <> a écrit :
you can eventually test:
    if (t.pending_payments[i] == nil) then break end
inside your loop (if you assume that the list of months has no "hole", but you don't want to set a limit for your for loop.

Anyway the for loop should better be using ipairs() so that you can still accept "holes" in months:

  for i,payment in in ipairs(t) do
    if limit and i > limit then break end
    total = total + payment.amount;

(your sample only shows named months probably of the same year, but the actual data may be an arbitrary month in ISO format "yyyy-mm" or using a second property for the year. In which case the "limit" should be able to compute if the date match the limit which could accept either a maximum number of months over possibly several years, or a date limit also in "yyyy-mm" format).
Also nothing indicates that the JSON data is necessary ordered: eahc payment has its own month, they may occur in arbitrary order, or there would be several payments for the same month in different rows...
So check your data model for your JSON source!

Le ven. 12 juin 2020 à 19:17, <> a écrit :
Thank you Francisco.

You were absolutely right, the limit was the problem. I found the delay had nothing to do with the calculation in LUA. It was related with a configurable delay in the retrieval of the limit variable.

Thanks very much for your reply!

Wilmar Pérez