Beginner Help Needed

Want Cheat Engine to do something specific and no idea how to do that, ask here. (From simple scripts to full trainers and extensions)
User avatar
++METHOS
Administration
Administration
Posts: 275
Joined: Thu Mar 02, 2017 9:02 pm
Reputation: 95

Beginner Help Needed

Post by ++METHOS »

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: Select all

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.

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

It sounds like another loop with ipairs should worK..

Code: Select all

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

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

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.

User avatar
++METHOS
Administration
Administration
Posts: 275
Joined: Thu Mar 02, 2017 9:02 pm
Reputation: 95

Re: Beginner Help Needed

Post by ++METHOS »

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!

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

Sorry you were comparing a number to a string. forgot the tonumber function

Code: Select all

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.

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

Though I remember pairs returning integers as keys, so this my work.

Code: Select all

-- It wasn't right keep going.. Sorry
But ipairs looks exclusively for integer key.
Last edited by TimFun13 on Fri Mar 03, 2017 10:21 pm, edited 1 time in total.

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

You may need to remove based on integer key.

Code: Select all

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

User avatar
++METHOS
Administration
Administration
Posts: 275
Joined: Thu Mar 02, 2017 9:02 pm
Reputation: 95

Re: Beginner Help Needed

Post by ++METHOS »

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: Select all

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

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

File io in lua is a little different. But still much the same as most other languages.

Code: Select all


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: Select all


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


Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

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: Select all

os.execute('mkdir someDirectory')

User avatar
++METHOS
Administration
Administration
Posts: 275
Joined: Thu Mar 02, 2017 9:02 pm
Reputation: 95

Re: Beginner Help Needed

Post by ++METHOS »

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.

Speedwagonz
Noobzor
Noobzor
Posts: 13
Joined: Thu Mar 02, 2017 11:15 pm
Reputation: 0

Re: Beginner Help Needed

Post by Speedwagonz »

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.

User avatar
++METHOS
Administration
Administration
Posts: 275
Joined: Thu Mar 02, 2017 9:02 pm
Reputation: 95

Re: Beginner Help Needed

Post by ++METHOS »

ShyTwig16 wrote:
Fri Mar 03, 2017 11:34 pm
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?
What is cheating?
Posts: 2
Joined: Sat Mar 04, 2017 2:03 am
Reputation: 0

Re: Beginner Help Needed

Post by DaSpamer »

Some small table search script that I wrote several month ago,
Based on my answer on [Link]

Code: Select all

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: Select all

	--[[-- 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: Select all

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: Select all

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: Select all

YOURPATH = "%APPDATA%\\blah"
shellExecute('cmd.exe', '/c mkdir "' .. YOURPATH .. '"', nil, false);

let me know if you got any questions.

User avatar
++METHOS
Administration
Administration
Posts: 275
Joined: Thu Mar 02, 2017 9:02 pm
Reputation: 95

Re: Beginner Help Needed

Post by ++METHOS »

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.

Post Reply

Who is online

Users browsing this forum: No registered users