maikyy
maikyy

Reputation: 13

Modulo alternative Lua

I don't have much coding experience so I don't really know of an efficient alternative to modulo, the issue I have is that I want to have the same funcionality but witouth it ever returning zero if that makes sense.

So I have an arbritary value % 8 and I want my results to go (1,2,3,4,5,6,7,8,1,2,3,etc)

any help or push in the right direction would be appreciated.

Upvotes: 1

Views: 515

Answers (2)

Luatic
Luatic

Reputation: 11191

I assume you're trying to make indices from 1 to 8 loop. For zero-based offsets from 0 to 7 this would be trivial by using i % 8; consider simply making your table zero-based.

For one-based indices, the simplest way to go is to first subtract 1 to make it zero-based, then apply the modulo to wrap around, then add 1 to make it one-based again: ((i - 1) % 8) + 1.

Upvotes: 2

Piglet
Piglet

Reputation: 28954

So I have an arbritary value % 8 and I want my results to go (1,2,3,4,5,6,7,8,1,2,3,etc)

local result = value % 8 + 1

This is a simple maths problem. If one arrithmetic operator doesn't give you the desired result, use or add others to your formula.

Upvotes: 1

Related Questions