Announcement

Collapse
No announcement yet.

Get integers via for Loop or Array Need Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • telco
    replied
    Thanks bio.. just got home from vacation.. i already see this last week but can managed to reply i cant login using other computer.. i forgot my password lol...

    Leave a comment:


  • BioHazard
    replied
    No problemo, buddy.

    Just break the problem down into small, individual steps. And then think logically about how to address each individual step. Sometimes I find it helpful to use old-fashioned pencil & paper to sketch out the logic in the form of flow-charts. Helps me keep the mental-gymnastics straight in my head. Try it, you'd be surprised how much it can help sometimes.

    So, using a Count-Loop.
    Like this:
    Code:
    nTotal = 0
    for count = 1, 3 do
        nReturn = Paragraph.GetText("Paragraph"..count);
        nTotal = nTotal + nReturn
    end
    Paragraph.SetText("Total", nTotal);
    Or, using a Table-Array.
    Like this:
    Code:
    tNumbers = {
        Paragraph.GetText("Paragraph1"),
        Paragraph.GetText("Paragraph2"),
        Paragraph.GetText("Paragraph3"),
    };
    
    nTotal = 0
    for k,v in pairs (tNumbers) do
        nTotal = nTotal + v
    end
    Paragraph.SetText("Total", nTotal);
    ............................

    Nb.
    Regarding the use of String.ToNumber:

    In this particular case, string as number is implicit.
    From: https://www.lua.org/pil/2.4.html

    Lua provides automatic conversions between numbers and strings at run time. Any numeric operation applied to a string tries to convert the string to a number ...
    Note: this does not apply with comparisons.
    ie
    Code:
    if "10" > 10 then
    .. in which case, string to number conversion is required for interpretation as an integer value.
    Attached Files

    Leave a comment:


  • telco
    started a topic Get integers via for Loop or Array Need Help

    Get integers via for Loop or Array Need Help

    I have paragraph's that contains a number, i want to get and add those numbers, here is my implementation i will just use 3 paragraph for example.

    par1 = String.ToNumber(Paragraph.GetText("Paragraph1"))
    par2 = String.ToNumber(Paragraph.GetText("Paragraph2"))
    par3 = String.ToNumber(Paragraph.GetText("Paragraph3"))

    Total = par1+par2+par3

    so that total will be the sum of 3 paragraph.
    But how to achieve it using for loop for array? i am now practising Loop or array is there way to get those value in Array or for Loop ways?

    i try this in array:
    Total = { "Paragraph1", "Paragraph2", "Paragraph3"}

    newTotal = String.ToNumber(Total)
    Paragraph.SetText("Total", newTotal);

    not work


    also this

    for count =1, 3 do
    newTotal = String.ToNumber(Paragraph.GetText("Paragraph"..cou nt))
    end

    Paragraph.SetText("Total", newTotal);

    not work


    Thanks
Working...
X