Range function

Here’s the code for the range function. It almost works the same as Pythons range function, but produces a slightly different output. :slight_smile:

v1.01 added: I forgot to add “function setup()” in v1.00, so I have updated the code in this post.

Instructions: Enter three numbers into the range function, separated by a comma. The first number is the start value, the second number is the end value (inclusive), and the third number is the interval. The range function will then return a table containing a list of numbers using the values entered into the function.

-- Range function 1.01 (13/2/2015)

---------------------------------------------------------------------------

function range(Start, End, Interval)

	-- Create a table for adding the numbers into.

	local List = {}

	-- Create three variables that are to be used with a "for loop".
	-- If no parameters are entered into the function then "1" is used
	-- as the default value.

	local Start = Start or 1
	local End = End or 1
	local Interval = Interval or 1

	-- Create a list of numbers and return the result.

	for i = Start, End, Interval do
		table.insert(List, i)
	end

	return List

end

---------------------------------------------------------------------------

function setup()
    
    -- Create two number lists and assign them to variables "a" and "b".
    
    a = range(0, 50, 5)
    b = range(25, 1, -2)
    
    -- Print the two lists as strings with each number seperated by a comma.
    
    print(table.concat(a, ", "))
    print(table.concat(b, ", "))

end


Useful bit of code - thanks