LUA - For Loops

Hello,

I was wondering if I could get some help with this code below:

Example:

Table\_1 = {monthly\_income, home\_address} Table\_2 = {1,4) Table\_3 = {1,4) Table\_4 = {1,4) Table\_5 = {1,4) Table\_6 = {1,4) Table\_7 = {1,4) Table\_8 = {1,4) Table\_9 = {1,4) Table\_10 = {1,4) for i=1, table.getn(Table\_1) do if Table\_1[i] == "monthly\_income" then Reason\_Code1 = Table\_1[i] Reason\_Code2 = Table\_2[i] Reason\_Code3 = Table\_3[i] Reason\_Code4 = Table\_4[i] Reason\_Code5 = Table\_5[i] Reason\_Code6 = Table\_6[i] Reason\_Code7 = Table\_7[i] Reason\_Code8 = Table\_8[i] Reason\_Code9 = Table\_9[i] Reason\_Code10 = Table\_10[i] end if Table\_1[i] == "drivers\_license" then Reason\_Code11 = Table\_2[i] Reason\_Code12 = Table\_3[i] Reason\_Code13 = Table\_4[i] Reason\_Code14 = Table\_5[i] Reason\_Code15 = Table\_6[i] Reason\_Code16 = Table\_7[i] Reason\_Code17 = Table\_8[i] Reason\_Code18 = Table\_9[i] Reason\_Code19 = Table\_10[i] end if Table\_1[i] == "bank\_account" then Reason\_Code20 = Table\_2[i] Reason\_Code21 = Table\_3[i] Reason\_Code22 = Table\_4[i] Reason\_Code23 = Table\_5[i] Reason\_Code24 = Table\_6[i] Reason\_Code25 = Table\_7[i] Reason\_Code26 = Table\_8[i] Reason\_Code27 = Table\_9[i] Reason\_Code28 = Table\_10[i] end if Table\_1[i] == "home\_address" then Reason\_Code29 = Table\_2[i] Reason\_Code30 = Table\_3[i] Reason\_Code31 = Table\_4[i] Reason\_Code32 = Table\_5[i] Reason\_Code33 = Table\_6[i] Reason\_Code34 = Table\_7[i] Reason\_Code35 = Table\_8[i] Reason\_Code36 = Table\_9[i] Reason\_Code37 = Table\_10[i] end end

Output:

Reason_Code1 = 1

Reason_Code2 = 1

Reason_Code3 = 1

Reason_Code4 = 1

Reason_Code5 = 1

Reason_Code6 = 1

Reason_Code7 = 1

Reason_Code8 = 1

Reason_Code9 = 1

Reason_Code10 = 1

Reason_Code11 = 1

Reason_Code12 = 1

Reason_Code13 = 1

Reason_Code14 = 1

Reason_Code15 = 1

Reason_Code16 = 1

Reason_Code17 = 1

Reason_Code18 = 1

Reason_Code19 = 1

Reason_Code20 = 1

Reason_Code21 = 1

Reason_Code22 = 1

Reason_Code23 = 1

Reason_Code24 = 1

Reason_Code25 = 1

Reason_Code26 = 1

Reason_Code27 = 1

Reason_Code28 = 1

Reason_Code29 = 4

Reason_Code30 = 4

Reason_Code31 = 4

Reason_Code32 = 4

Reason_Code33 = 4

Reason_Code34 = 4

Reason_Code35 = 4

Reason_Code36 = 4

Reason_Code37 = 4

Reason_Code11 - Reason_Code28 should not have a value. 

Is my code correct?

Would really appreciate some help.

Thanks

Can you post the full code? I’m not completely understanding what the issue is right now.

I’ve updated the code. I don’t understand why the other reason_codes are being populated. Table_1 only has the values of monthly_income and home_address.

Is there a reason_code table which stores the values from reason_code[1] to reason_code[37]?

Yes. Reason_Code11 - Reason_Code28 should be blank but it is also getting the same values for monthly_income

After testing your code, I don’t see why you’re having issues. The code works just fine for me (after a little syntax clean up)

Reason\_Code = {} Table\_1 = {"monthly\_income", "home\_address"} Table\_2 = {1,4} Table\_3 = {1,4} Table\_4 = {1,4} Table\_5 = {1,4} Table\_6 = {1,4} Table\_7 = {1,4} Table\_8 = {1,4} Table\_9 = {1,4} Table\_10 = {1,4} for i=1, table.getn(Table\_1) do if (Table\_1[i] == "monthly\_income") then Reason\_Code[1] = Table\_2[i] Reason\_Code[2] = Table\_2[i] Reason\_Code[3] = Table\_3[i] Reason\_Code[4] = Table\_4[i] Reason\_Code[5] = Table\_5[i] Reason\_Code[6] = Table\_6[i] Reason\_Code[7] = Table\_7[i] Reason\_Code[8] = Table\_8[i] Reason\_Code[9] = Table\_9[i] Reason\_Code[10] = Table\_10[i] end if (Table\_1[i] == "drivers\_license") then Reason\_Code[11] = Table\_2[i] Reason\_Code[12] = Table\_3[i] Reason\_Code[13] = Table\_4[i] Reason\_Code[14] = Table\_5[i] Reason\_Code[15] = Table\_6[i] Reason\_Code[16] = Table\_7[i] Reason\_Code[17] = Table\_8[i] Reason\_Code[18] = Table\_9[i] Reason\_Code[19] = Table\_10[i] end if (Table\_1[i] == "bank\_account") then Reason\_Code[20] = Table\_2[i] Reason\_Code[21] = Table\_3[i] Reason\_Code[22] = Table\_4[i] Reason\_Code[23] = Table\_5[i] Reason\_Code[24] = Table\_6[i] Reason\_Code[25] = Table\_7[i] Reason\_Code[26] = Table\_8[i] Reason\_Code[27] = Table\_9[i] Reason\_Code[28] = Table\_10[i] end if (Table\_1[i] == "home\_address") then Reason\_Code[29] = Table\_2[i] Reason\_Code[30] = Table\_3[i] Reason\_Code[31] = Table\_4[i] Reason\_Code[32] = Table\_5[i] Reason\_Code[33] = Table\_6[i] Reason\_Code[34] = Table\_7[i] Reason\_Code[35] = Table\_8[i] Reason\_Code[36] = Table\_9[i] Reason\_Code[37] = Table\_10[i] end end for i = 1, 37 do print("Reason\_Code[" .. i .. "] = " .. tostring(Reason\_Code[i])) end 

Output:

Reason_Code1 = 1
Reason_Code2 = 1
Reason_Code3 = 1
Reason_Code4 = 1
Reason_Code5 = 1
Reason_Code6 = 1
Reason_Code7 = 1
Reason_Code8 = 1
Reason_Code9 = 1
Reason_Code10 = 1

Reason_Code11 = nil
Reason_Code12 = nil
Reason_Code13 = nil
Reason_Code14 = nil
Reason_Code15 = nil
Reason_Code16 = nil
Reason_Code17 = nil
Reason_Code18 = nil
Reason_Code19 = nil
Reason_Code20 = nil
Reason_Code21 = nil
Reason_Code22 = nil
Reason_Code23 = nil
Reason_Code24 = nil
Reason_Code25 = nil
Reason_Code26 = nil
Reason_Code27 = nil
Reason_Code28 = nil
Reason_Code29 = 4
Reason_Code30 = 4
Reason_Code31 = 4
Reason_Code32 = 4
Reason_Code33 = 4
Reason_Code34 = 4
Reason_Code35 = 4
Reason_Code36 = 4
Reason_Code37 = 4

You might have unknowingly initialized Reason_Code11 - Reason_Code28 before you ran the previous code segment.

Can you post the full code? I’m not completely understanding what the issue is right now.

I’ve updated the code. I don’t understand why the other reason_codes are being populated. Table_1 only has the values of monthly_income and home_address.

Is there a reason_code table which stores the values from reason_code[1] to reason_code[37]?

Yes. Reason_Code11 - Reason_Code28 should be blank but it is also getting the same values for monthly_income

After testing your code, I don’t see why you’re having issues. The code works just fine for me (after a little syntax clean up)

Reason\_Code = {} Table\_1 = {"monthly\_income", "home\_address"} Table\_2 = {1,4} Table\_3 = {1,4} Table\_4 = {1,4} Table\_5 = {1,4} Table\_6 = {1,4} Table\_7 = {1,4} Table\_8 = {1,4} Table\_9 = {1,4} Table\_10 = {1,4} for i=1, table.getn(Table\_1) do if (Table\_1[i] == "monthly\_income") then Reason\_Code[1] = Table\_2[i] Reason\_Code[2] = Table\_2[i] Reason\_Code[3] = Table\_3[i] Reason\_Code[4] = Table\_4[i] Reason\_Code[5] = Table\_5[i] Reason\_Code[6] = Table\_6[i] Reason\_Code[7] = Table\_7[i] Reason\_Code[8] = Table\_8[i] Reason\_Code[9] = Table\_9[i] Reason\_Code[10] = Table\_10[i] end if (Table\_1[i] == "drivers\_license") then Reason\_Code[11] = Table\_2[i] Reason\_Code[12] = Table\_3[i] Reason\_Code[13] = Table\_4[i] Reason\_Code[14] = Table\_5[i] Reason\_Code[15] = Table\_6[i] Reason\_Code[16] = Table\_7[i] Reason\_Code[17] = Table\_8[i] Reason\_Code[18] = Table\_9[i] Reason\_Code[19] = Table\_10[i] end if (Table\_1[i] == "bank\_account") then Reason\_Code[20] = Table\_2[i] Reason\_Code[21] = Table\_3[i] Reason\_Code[22] = Table\_4[i] Reason\_Code[23] = Table\_5[i] Reason\_Code[24] = Table\_6[i] Reason\_Code[25] = Table\_7[i] Reason\_Code[26] = Table\_8[i] Reason\_Code[27] = Table\_9[i] Reason\_Code[28] = Table\_10[i] end if (Table\_1[i] == "home\_address") then Reason\_Code[29] = Table\_2[i] Reason\_Code[30] = Table\_3[i] Reason\_Code[31] = Table\_4[i] Reason\_Code[32] = Table\_5[i] Reason\_Code[33] = Table\_6[i] Reason\_Code[34] = Table\_7[i] Reason\_Code[35] = Table\_8[i] Reason\_Code[36] = Table\_9[i] Reason\_Code[37] = Table\_10[i] end end for i = 1, 37 do print("Reason\_Code[" .. i .. "] = " .. tostring(Reason\_Code[i])) end 

Output:

Reason_Code1 = 1
Reason_Code2 = 1
Reason_Code3 = 1
Reason_Code4 = 1
Reason_Code5 = 1
Reason_Code6 = 1
Reason_Code7 = 1
Reason_Code8 = 1
Reason_Code9 = 1
Reason_Code10 = 1

Reason_Code11 = nil
Reason_Code12 = nil
Reason_Code13 = nil
Reason_Code14 = nil
Reason_Code15 = nil
Reason_Code16 = nil
Reason_Code17 = nil
Reason_Code18 = nil
Reason_Code19 = nil
Reason_Code20 = nil
Reason_Code21 = nil
Reason_Code22 = nil
Reason_Code23 = nil
Reason_Code24 = nil
Reason_Code25 = nil
Reason_Code26 = nil
Reason_Code27 = nil
Reason_Code28 = nil
Reason_Code29 = 4
Reason_Code30 = 4
Reason_Code31 = 4
Reason_Code32 = 4
Reason_Code33 = 4
Reason_Code34 = 4
Reason_Code35 = 4
Reason_Code36 = 4
Reason_Code37 = 4

You might have unknowingly initialized Reason_Code11 - Reason_Code28 before you ran the previous code segment.