Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lots of problems I'm seeing in this. First, I'd never rely on <code>i</code> being set in an external function, it really should be a local variable and passed as a parameter if you need it. That said, you need to check if <code>no_table[x]</code> exists before trying to access <code>no_table[x][y]</code>. So, for <code>foo</code> you'd have:</p> <pre><code>function foo(a,b,c,d) if not (no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4]) then print (a+b) elseif no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4] then print (b+c) end end </code></pre> <p>Also, for numbers in the table, if you want to do arithmetic, you need to remove the quotes:</p> <pre><code>no_table ={ {a=3, b=22, c=18, d="ABC"}, {a=4, b=12, c=25, d="ABC"}, {a=5, b=15, c=16, d="CDE"}, } </code></pre> <p>Next, in <code>testfoo</code>, you're passing a table, so you either need to split out the values of a, b, c, and d on your function call, or you can just pass the table itself and handle that in foo:</p> <pre><code>function foo(t) if not (no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4]) then print (t.a+t.b) elseif no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4] then print (t.b+t.c) end end </code></pre> <p>This results in:</p> <pre><code>&gt; testfoo() 25 37 31 </code></pre> <p>Edit: One final cleanup, since the conditions are the same, you can use an <code>else</code> rather than an <code>elseif</code>:</p> <pre><code>function foo(t) if no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4] then print (t.b+t.c) else print (t.a+t.b) end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload