My blog

Very occasional opinions about almost anything

Using recursive functions to return a table visulized as a string in Lua

Friday 20th October 2017 at 21:40pm

Trying to print a table in Lua will give you something like table: 1x0324421.
This function takes a table and returns it as it would appear in code.

function tableAsString(tbl)
	toReturn = "{"
	for ind, val in pairs(tbl) do
		if type(val) == "table" then
			toReturn = toReturn .. (type(ind) == "number" and "" or ind .. "=") .. tableAsString(val) .. ","
		else
			local newVal
			if type(val) == "string" then
				newVal = "\"" .. val .. "\""
			else
				newVal = val
			end
			toReturn = toReturn .. (type(ind) == "number" and "" or ind .. "=") .. newVal .. ","
		end
	end
	
	toReturn = toReturn:sub(1,-2) .. "}" -- remove trailing "," and close table
	
	return toReturn
end
Since every table starts with a "{", this is the start of the returned string.
After this, every value is looped through. If it's a value (string/number) it's appended to the return string, with it's index if it's not a number (numbers are considered to be automatic indices). If it's a table, then it needs to be displayed as one, so instead of appending the value, we need a table string: so we recursively call the function with the table that needs to be a string as the parameter. If that table contains more tables, they will be dealt with recursively. Finally, before closing the table, any trailing commas are removed.


Scale choosing tool

Saturday 12th March 2016 at 19:54pm

Have an instrument exam coming up? Try my scale chooser to test yourself. Currently, it has a limited selection of grades, but when I can, I will add more grades and hopefully instruments. Try it out!


BLiP is here!

Thursday 3rd March 2016 at 13:44pm

BLiP is now useable! Here's what it is:

BLiP is a way to play and listen to music and videos with other people, in real time. Create a stream, and suggest music and videos to play next, or just listen to what everyone else is playing. Think of it as a way to hang out with other people and listen to some good music.

You can find it here: blip.jamesthistlewood.co.uk.

If you find any bugs or want to suggest any features, please contact me.


Biscuitpedia

Saturday 19th December 2015 at 16:37pm

The Christmas holiday has just rolled around, and I was bored this morning. What better thing is there to do than to create a biscuit encyclopedia? So, I set about making it happen. It's called Biscuitpedia, and you can find it here. You don't even need to sign up to edit and create pages!
Merry Christmas!

By the way, it's not meant to be a serious wiki as such, but serious and joking contributions are fine. However, edits are not backed up - there is no edit log - so please don't go about removing content and breaking pages; I don't want to have to start collecting and banning IPs and requiring a user login.


Bibliography generator

Saturday 12th December 2015 at 18:17pm

EDIT: This is broken, and I can't be bothered to fix it so for the time being it won't work. Have you ever only used one website for an assignment? Do you want to make it look like you've looked at a variety of different sources when you've just found one really useful website? I made a bibliography generator, which grabs four websites from Google according to your search query and formats them as a bibliography.

I might release the source code, but it's pretty simple anyway.


A new design

Sunday 29th November 2015 at 21:27pm

Well, after a considerable time with this very bad website layout, I decided to work on a new one. I present: jamesthistlewood.co.uk, with a new, clean, responsive design. There will be bugs, and I haven't really decided what to put on it yet apart from this blog, so I guess that's all I have to say.