...
local tmpPos = {x = position.x, y = position.y, z = position.z, stackpos = -1}
local tileCount = getTileThingByPos(tmpPos)
local i = 1
local tmpItem = {uid = 1}
while(tmpItem.uid ~= 0 and i < tileCount) do
tmpPos.stackpos = i
tmpItem = getTileThingByPos(tmpPos)
if(tmpItem.uid ~= item.uid and tmpItem.uid ~= 0 and isMoveable(tmpItem.uid)) then
doRemoveItem(tmpItem.uid)
else
i = i + 1
end
end
...