This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Why is final always returning nil?

Started by mrpoopy345, 05 March 2015 - 12:57 PM
mrpoopy345 #1
Posted 05 March 2015 - 01:57 PM
I am trying to make a piece of code that takes a string and outputs that string up to where the first vowel is.
For example,

thumb
Would become:

th
Here is my current code:

first = io.read()
sec = io.read()
vowels = {"a", "e", "i", "o", "u"}
for i =1, string.len(first) do
l = string.sub(first, i, i)
for k, v in pairs(vowels) do
if l == v then
if final ~= nil then --Need this or it will go up to the last vowel
final = string.sub(first, 1, i-1)
end
end
end
end
print(final)
But final is always nil for some reason. Any help?
KingofGamesYami #2
Posted 05 March 2015 - 02:11 PM
if final ~= nil then --Need this or it will go up to the last vowel

You never define final before this if statment, meaning it will never be true, meaning it will always skip everything inside it.

Edit: easiest way to do this:


local function firstVowel( str )
  return str:match( "[^aeiou]+" )
end
Edited on 05 March 2015 - 01:15 PM
GopherAtl #3
Posted 05 March 2015 - 02:12 PM
you never initialize final. "if final~=nil" will never be true. I think you meant that to be "if final==nil" which would update final only if it hadn't been set already. As it is now, it sets final only if it has been been set already, which naturally can never happen!

You must also initialize final to nil before both loops. Because of how globals work in lua, it's currently keeping it's value between runs of the program, so long as the computer isn't rebooted!


first = io.read()
sec = io.read()
final=nil

vowels = {"a", "e", "i", "o", "u"}
for i =1, string.len(first) do
  l = string.sub(first, i, i)
  for k, v in pairs(vowels) do
	if l == v then
	  if final == nil then --Need this or it will go up to the last vowel
		final = string.sub(first, 1, i-1)
	  end
	end
  end
end
print(final)

:edit: This is a modified version of the same program that has some suggested improvements, with comments explaining them. For educational purposes.

--// it's a good idea to make all variables local, by prefixing with "local"
--// when you're setting their initial value. Prevents them carrying over to
--// other programs and causing weird results and bugs!
local first = io.read()
local sec = io.read()

local final = nil

--// if you make the vowels keys into a mapped table instead of values in an array table,
--// you can eliminate the inner loop over the vowels
vowels = {   ["a"] = true,   ["e"] = true,   ["i"] = true,   ["o"] = true,   ["u"] = true, }

--// the # operator returns length of a string or array, shorter than doing string.len
for  i = 1, #first do
  l = string.sub(first,i,i)
  --// with the keyed table, we can just do this
  if vowels[l] then
	final = string.sub(first,1,i-1)
	--// since we only have the one loop now, we can just break out
	break
  end
end

print(final)
Edited on 05 March 2015 - 01:22 PM