Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
superbacon

Library not loading?

Question

I've been working on this library to automatically change decimal to hexadecimal, but i've been running into a few problems. The first one being that for some reason when i try to load the library, it returns;

home/test; 1; module 'hex' not found

    no field package.preload['hex']

    no field package.delayed['hex']

/lib/hex.lua; 3; <name> expected near '='

Here's the code:

hex = {}

local function = hex.toHex(input)
	local a = input/16
	local b = a/16
	local c = b/16
	local d = c/16
	local e = d/16
	local g = input%16
	local h = a%16
	local i = b%16
	local j = c%16
	local k = d%16
	local l = e%16
	if g,h,i,j,k,l < 9 then
		if g = 10 then
			g = "a"
		end
		if g = 11 then
			g = "b"
		end
		if g = 12 then
			g = "c"
		end
		if g = 13 then
			g = "d"
		end
		if g = 14 then
			g = "e"
		end
		if g = 15 then
			g = "f"
		end
		if h = 10 then
			h = "a"
		end
		if h = 11 then
			h = "b"
		end
		if h = 12 then
			h = "c"
		end
		if h = 13 then
			h = "d"
		end
		if h = 14 then
			h = "e"
		end
		if h = 15 then
			h = "f"
		end
		if i = 10 then
			i = "a"
		end
		if i = 11 then
			i = "b"
		end
		if i = 12 then
			i = "c"
		end
		if i = 13 then
			i = "d"
		end
		if i = 14 then
			i = "e"
		end
		if i = 15 then
			i = "f"
		end
		if j = 10 then
			j = "a"
		end
		if j = 11 then
			j = "b"
		end
		if j = 12 then
			j = "c"
		end
		if j = 13 then
			j = "d"
		end
		if j = 14 then
			j = "e"
		end
		if j = 15 then
			j = "f"
		end
		if k = 10 then
			k = "a"
		end
		if k = 11 then
			k = "b"
		end
		if k = 12 then
			k = "c"
		end
		if k = 13 then
			k = "d"
		end
		if k = 14 then
			k = "e"
		end
		if k = 15 then
			k = "f"
		end
		if l = 10 then
			l = "a"
		end
		if l = 11 then
			l = "b"
		end
		if l = 12 then
			l = "c"
		end
		if l = 13 then
			l = "d"
		end
		if l = 14 then
			l = "e"
		end
		if l = 15 then
			l = "f"
		end
		local hexnum = tonumber(l..k..j..i..h..g)
		return hexnum
	end
	return hex

Sorry it's so long!

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 1

First, replace local function = hex.toHex(input) to function hex.toHex(input).

Second, you can't use = in if condition. If you want to check for equality, use ==.

Third, you must end the function with the end operator.

Fourth, the return hex line is incorrect. You didn't define the hex variable so the line will make the function return nil.

Fifth, you can't do if a,b,c < 42 then. You must check every variable and then "sum" the answer using and, or, not. E.g., a < 42 and b < 42 and c < 42 checks that a, b, c all less than 42.

Sixth, this is the most inefficient dec-to-hex algorithm I've seen so far. The whole function can be replaced with this:

function hex.toHex(num)
  return ("%x"):format(num)
end

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.