The following code creates an array of boxes to represent an array.
\startMPinclusions
input boxes ;
\stopMPinclusions
\starttext
\startluacode
function createGraphic(k,c,indH)
local i=1
local j=65
context.startuseMPgraphic(name)
context("boxjoin(a.se=b.sw; a.ne=b.nw);")
while k[i] do
bname=string.char(j)
context("boxit."..bname.."(btex "..k[i].." etex)("..c[i]..");drawboxed("..bname..");")
i=i+1
j=j+1
end
context.stopuseMPgraphic()
context.useMPgraphic(name)
end
a={51,31,4,22,23,45,23,43,54,22,11,34}
colors={"blue","blue","blue","blue","blue","blue","blue","blue","blue","white","white","white"}
createGraphic(a,colors)
The output is

Now I want to change the colors dynamically and call the createGraphic() in a loop. So I wrote the following function to change the colors array.
function refreshColors(yellowEndIndex,redIndex,blueIndex)
print(yellowEndIndex..","..redIndex..","..blueIndex)
local ccnt=1
while a[ccnt] do
if ccnt < yellowEndIndex then
colors[ccnt] = "yellow"
elseif ccnt == redIndex then
colors[ccnt] = "red"
elseif ccnt == blueIndex then
colors[ccnt] = "blue"
else
colors[ccnt] = "white"
end
ccnt= ccnt+1
end
end
refreshColors(2,4,6)
createGraphic(a,colors)
Now, the output is

Also when I call refreshColors() in loop and createGraphic() outside there is no error.
for i=1,10,1 do
refreshColors(i,0,0)
end
createGraphic(a,colors)
Now the output is

However, when I call createGraphic() inside loop, error occurs.
for i=1,10,1 do
refreshColors(i,0,0)
createGraphic(a,colors)
end
The error message thrown by Texworks is
error on line 33 in file try.tex: terminal: ! Inconsistent equation (off by -1).
Line 33 is
colors[ccnt] = "red"
Here is the link to the boxes.mp used in the program.
I want to call the createGraphic() inside the loop. Why the error occurs??

`to mark your inline code as I did in my edit. – hpesoj626 Feb 12 at 4:37\stopluacode, etc., is missing) and I haven't managed to recreate code usingcreateGraphic()that runs without error. Could you post a proper MWE? I note that you are passing the uninitialised valuenameto startuseMPgraphic - this is anilvalue where it should be a string, though I doubt that is the problem. – Charles Stewart Feb 12 at 12:08