Beginner Help Needed

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#1
I am trying to learn Lua. I have a test script that I am working on that creates a to-do list. I am trying to implement the ability to remove items based on index number AND value, but I cannot figure out how to do both:
Code:
reminders={}

while input~="add" .. "remove" .. "save" .. "exit" do
	print("Reminders:")
		for i,v in pairs(reminders) do
			print(i,v)
		end

	print("Available commands: (add / remove / save / exit)")
	input=io.read()

	if input=="add" then
		print("What do you want to add")
		item=io.read()
		table.insert(reminders,item)

	elseif input=="remove" then
		print("What do you want to remove?")
		item=io.read()
			for i,v in pairs(reminders) do
				if v==item then
					table.remove(reminders,i)
				end
				--elseif v~=item then
					--table.remove(reminders,item)
				--end
			end
	end

	if input=="save" then
		print("This feature has not been implemented.")
	end

	if input=="exit" then
		os.exit()
	end
end
Thanks.
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#2
It sounds like another loop with ipairs should worK..
Code:
reminders={}

while input~="add" .. "remove" .. "save" .. "exit" do
	print("Reminders:")
		for i,v in pairs(reminders) do
			print(i,v)
		end

	print("Available commands: (add / remove / save / exit)")
	input=io.read()

	if input=="add" then
		print("What do you want to add")
		item=io.read()
		table.insert(reminders,item)

	elseif input=="remove" then
		print("What do you want to remove?")
		item=io.read()
			for i,v in pairs(reminders) do
				if v==item then
					table.remove(reminders,i)
				end
				--elseif v~=item then
					--table.remove(reminders,item)
				--end
			end
			for i, v in ipairs(reminders) do
				if i==item then
					table.remove(reminders,i)
				end
				--elseif v~=item then
					--table.remove(reminders,item)
				--end
			end
	end

	if input=="save" then
		print("This feature has not been implemented.")
	end

	if input=="exit" then
		os.exit()
	end
end
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#3
You may want to put the ipairs loop first so the index will be as expected if allowing both index and value based removal at the same time.
 

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#4
Thanks for replying.

I have tested your code, but I am still only able to remove items based on one or the other -- in this case, based on name/value. For example, if this is my list:

1. red
2. blue
3. green
4. yellow
5. orange

I can remove index item number 3, by typing in green. But, I cannot remove it by typing 3.

Also, I did notice that you are using ipairs in lieu of pairs. Can you explain the difference or purpose for using one over the other?

Thanks!
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#5
Sorry you were comparing a number to a string. forgot the tonumber function
Code:
reminders={}

while input~="add" .. "remove" .. "save" .. "exit" do
	print("Reminders:")
		for i,v in pairs(reminders) do
			print(i,v)
		end

	print("Available commands: (add / remove / save / exit)")
	input=io.read()

	if input=="add" then
		print("What do you want to add")
		item=io.read()
		table.insert(reminders,item)

	elseif input=="remove" then
		print("What do you want to remove?")
		item=io.read()
			for i,v in pairs(reminders) do
				if v==item then
					table.remove(reminders,i)
				end
				--elseif v~=item then
					--table.remove(reminders,item)
				--end
			end
			for i, v in ipairs(reminders) do
				if i == tonumber(item) then
					table.remove(reminders,i)
				end
				--elseif v~=item then
					--table.remove(reminders,item)
				--end
			end
	end

	if input=="save" then
		print("This feature has not been implemented.")
	end

	if input=="exit" then
		os.exit()
	end
end
As for pairs and ipairs basicly pairs looks for string keys and ipairs looks for integer keys.
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#6
Though I remember pairs returning integers as keys, so this my work.
Code:
-- It wasn't right keep going.. Sorry
But ipairs looks exclusively for integer key.
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#7
You may need to remove based on integer key.
Code:
reminders={}

while input:lower() ~= 'add' .. 'remove' .. 'save' .. 'exit' do
	print('Reminders:')
	for i, v in pairs(reminders) do
		print(i,v)
	end

	print('Available commands: (add / remove / save / exit)')
	input = io.read()

	if input:lower() == 'add' then
		print('What do you want to add')
		item = io.read()
		table.insert(reminders,item)
	elseif input:lower() == 'remove' then
		print('What do you want to remove?')
		item = io.read()
		for k, v in pairs(reminders) do
			if v == item or k == tonumber(item) then
				table.remove(reminders, k)
			end
		end
	end

	if input:lower() == 'save' then
		print('This feature has not been implemented.')
	end

	if input:lower() == 'exit' then
		os.exit()
	end
end
 

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#8
Thank you for your help. Converting item to number and comparing it against i seemed to do the trick. Having pairs or ipairs does not seem to matter.

This code is working:
Code:
reminders={}

while input~="add" .. "remove" .. "save" .. "exit" do
	print("Reminders:")
		for i,v in pairs(reminders) do
			print(i,v)
		end

	print("Available commands: (add / remove / save / exit)")
	input=io.read()

	if input=="add" then
		print("What do you want to add")
		item=io.read()
		table.insert(reminders,item)

	elseif input=="remove" then
		print("What do you want to remove?")
		item=io.read()
			for i,v in pairs(reminders) do
				if v==item then
					table.remove(reminders,i)
				end
			end

			for i, v in pairs(reminders) do
				if i==tonumber(item) then
					table.remove(reminders,i)
				end
			end
	end

	if input=="save" then
		print("This feature has not been implemented.")
	end

	if input=="exit" then
		os.exit()
	end
end
Thanks again. Next step will be learning how to write/create file and save to hard disk. :D
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#9
File io in lua is a little different. But still much the same as most other languages.
Code:
local file, err = io.open('somefile.txt', 'w')
if file and not err then
	io.output(file)
	io.write('Some cool stuff')
	file:write('Is going down!') -- Can't remember if this append line ends for not I think I does.
	io.close(file)
elseif err then
	print(err)
end

local file, err = io.open('somefile.txt', 'r')
if file and not err then
	print(file:read())
	file:close()
elseif err then
	print(err)
end
and here are some helpers I use all the time for file io (pulled form a google search).
Code:
function getPath( ... )
	local pathseparator = package.config:sub(1,1)
	local elements = { ... }
	return table.concat(elements, pathseparator)
end

local function exists(path)
	if type(path) ~= 'string' then return end
	return os.rename(path, path) and true or false
end

local function isFile(path)
	if type(path) ~= 'string' then return end
	if not exists(path) then return false end
	local f = io.open(path)
	if f then
		f:close()
		return true
	end
	return false
end

local function isDirectory(path)
	return (exists(path) and not isFile(path))
end
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#10
Creating a directory is usually the most difficult part. Most use lua file systems (lfs), but here is a hacky why to get it done.
Code:
os.execute('mkdir someDirectory')
 

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#11
Thanks! I hope you do not mind if I will refrain from reading your comments until I have tried myself. :mrgreen:

I will report back my findings after I have had time to work on this some more.
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#12
Total understand, sorry for spoilers..
I learn best by trying and doing, not by being told, so I think I know what you mean.
Plus some times you just want to see what you will come up with verses others.
 

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#13
TheyCallMeTim13 post_id=359 time=1488584075 user_id=91 said:
Plus some times you just want to see what you will come up with verses others.
-I have just started, so I am not even close to that point yet. :D
But, if I do see that you did something differently, it sometimes forces me to learn new things.
 

DaSpamer

What is cheating?
Mar 4, 2017
2
0
1
#14
Some small table search script that I wrote several month ago,
Based on my answer on stackoverflow
Code:
function inArray(array,input,index)
	if (type(array) ~= 'table') then
		return false;
	end
	for key,value in pairs(array) do
		if (type(value)=='table') then
			local status = inArray(value,input,index);
			if (status) then
				return status
			end
		-- elseif ((tonumber(input) or input) == (tonumber(value) or value) and (not index and key == index or true)) then
			elseif (((input == value) or (tonumber(input) == tonumber(value))) and (not index and key == index or true)) then
			return true;
		end
	end
	return false
end
function table.search(input,value,case,index_check,_return,returnall,exclude) -- some utility i wrote for someone long time ago
	-- print("Start search",tostring(input))
	-- processMessages();
	-- sleep(100);
	local cache,info,foundresults,func = {},{},false
	local tostring,type = tostring,type
	local output = function(input,value,case,index_check,_return, returnall,exclude)
		if (type(input)~='table') then
			return nil;
		else
			local ts_input = tostring(input);
			local cur_cache = cache[ts_input];
			if (cur_cache) then
				return false;
			end
			if (exclude and type(exclude) == 'table' and inArray(exclude,input)) then
				return false;
			end
			cache[ts_input] = ts_input
			if (type(value) == 'table' and value == input) then
				return true;
			end
			for key,object in pairs(input) do
				if (index_check) then
					if (case == "find" and type(value)=='string' and type(key)=='string') then
						if (key:lower():find(value:lower())) then -- to avoid exit the loop
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						end
					elseif (case and type(value)=='string' and type(key)=='string') then
						if (value:lower() == key:lower()) then -- to avoid exit the loop
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						end
					else
						if (key == value) then
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						elseif(type(object)=='table' and not cache[tostring(object)]) then
							local result = func(object,value,case,index_check,_return, returnall,exclude)
							if (result and not returnall) then
								return result;
							end
						end
					end
				else
					if (case == "find" and type(value)=='string' and type(object) == 'string') then
						if (object:lower():find(value:lower())) then
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						end
					elseif (case and type(value)=='string' and type(object) == 'string') then
						if (value:lower() == object:lower()) then
							-- cache = {};
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						end
					elseif(type(object)=='table' and not cache[tostring(object)]) then
						if (value == object) then
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						else
							local result = func(object,value,case,index_check,_return, returnall,exclude)
							if (result and not returnall) then
								return result;
							end
						end
					else
						if (object == value) then
							if (returnall) then
								foundresults = true;
								info[ts_input] = input
							else
								return (_return and input or true);
							end
						end
					end
				end
			end
		end
		if (foundresults) then
			return info,true;
		else
			return false;
		end
	end
	func = function (...) return output(...) end;
	local op = output(input,value,case,index_check,_return, returnall,exclude);
	cache = {};
	return op
end
Usage is simple, but this function makes lives easier and fairly fast (unless you run an array with thousands keys and has more than 3 dimensions, it should work very fast).
usage:
Code:
	--[[-- table.search(
						@param1 table(table),
						@param2 value(value to search),
						@param3 case-sensitive(true/false),
						@param4 type (index or value type)
						@param5 return relative table
						@param6 return relative table, continues the search and returns all data
						@param7 exclude table (to be checked using a simple inArray function)
					)
		-- checks if these two indexes were set any where in the launchArgs table and checks their type
		if (table.search(t,"hi",true)) then-- table,name,case(true to ignore case)
		-- do your stuff
		end
	-- ]]
So you may use this function to search based key inside your table, and search based on value, and since this function returns the table in which you've found in your searches, if both results equal it's likely to be the very same table, but just in case it isn't it, recommended defining uniquely your table by any key or any value so results would be more accurate.
In short words
Code:
table.search(check key) == table.search(check value entry) or table.search(unique key)
table.search result is the parent table (or the supplied table) of the property or key value where input was found.
So doing:
Code:
bla = table.search( ...)
for k in ipairs(bla) do -- use ipairs when you index by numbers, otherwise use pairs.
	bla[k] = nil -- nils everything on that table, to delete it you must remove any reference of it, 
end		-- calling garagecollector will force c.e to free it immediately assuming there is no refernce to it.
Some quick method to create a dir (Silently).
Code:
YOURPATH = "%APPDATA%\\blah"
shellExecute('cmd.exe', '/c mkdir "' .. YOURPATH .. '"', nil, false);

let me know if you got any questions.
 

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#15
DaSpamer-
:D That stuff is way beyond my current comprehension level haha. I do appreciate you taking the time, however, and I will try to study your work as I progress.

Thank you.
 

DaSpamer

What is cheating?
Mar 4, 2017
2
0
1
#16
A little bit offtopic
The table.search function provided here is updated and much more optimized, (based on my test in that post, using very same laptop model, or desktop using i7-4790k @4GHz).
It seems that table.search is 2-3 times much faster, thus this function could be handy almost in any situation, and I really hope you guys make use of it.
I commented new timestamps, along with the older previous test ones.
Code:
-- i7-4720HQ @3.5GHz 4Cores
local start_time = os.clock();
t = {}
for i=1,500000 do --500k table size
    t[i]=i-1
end
print(os.clock()-start_time) -- 0.0280000.. -- 0.05 sec to create the table
local start_time = os.clock();
print(tostring(table.search(t,500000,false,true)))--true -- will try to find a key with the value of 500,000
print(os.clock()-start_time) -- 0.08799999.. -- 0.197sec sec to scan the whole table

function nestedTable(object,times) -- creates nested table -> object={{{{..n times}}}}
    if (times > 0 ) then
        object[#object+1] = {times = times}
        return (nestedTable(object[#object],times-1))
    end
end
local start_time = os.clock();
t = {};
nestedTable(t,15000) --> will create table inside a table x 15000 times.
print(os.clock()-start_time) -- 0.017000 .. --0.007 sec to create the nested table
local start_time = os.clock();
print(tostring(table.search(t,1,false)))-- true -- will try to find a 1 (as a table value), the very last table value
print(os.clock()-start_time) -- 0.04199999.. -- 0.014 sec to find the value in the nested table


I recommend using the code as it is, It's very simple, basically iterates over the whole table and it's childs childs childs...
 

++METHOS

Expert Cheater
Mar 2, 2017
202
1
18
#17
Thanks, DaSpamer.

I wish I could make more sense of what you wrote here. I will be sure to study it more and hopefully make use of it in the future. :mrgreen:
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
428
47
28
#18
DaSpamer post_id=386 time=1488594893 user_id=559 said:
Some quick method to create a dir (Silently).
Code:
YOURPATH = "%APPDATA%\\blah"
shellExecute('cmd.exe', '/c mkdir "' .. YOURPATH .. '"', nil, false);
Thank you been kinda trying to do that, with lua.
 

Darkenki

What is cheating?
Apr 6, 2018
1
0
1
#20
I am a noob I am trying but it seems I cant get it to work or even make it recognize commands
when I speak it does not recognize either ...
Also what benifits do i get if I upgrade to mark 3 ???

I also need to know from where can i get facebook login and logout if i can get a script and a step by step guide ?
pls help
 
Top Bottom