Thread Tools Display Modes
05/19/23, 10:49 AM   #1
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 244
Question tables and string.find help

hey guys,

Im messing around with tables for a new feature mostly to understand these better and I hit a wall. I keep getting nil as a returned value and I cant see why.

Code:
function MyAddon.FakeFunction()
     MyAddon.savedVariables.autoMetSwap = "Master Gatherer"
     local swapSkill = MyAddon.FindChampionSwapSkill()
     df(tostring(swapSkill))
end

function MyAddon.FindChampionSwapSkill()
	--/script  df(GetChampionSkillName(number))
	local championSkills = {
		["Master Gatherer"] = 78,
		["Treasure Hunter"] = 79,
		["Gifted Rider"] = 92,
		["Homemaker"] = 91,
		["Cutpurse's Art"] = 90,
		["Infamous"] = 77,
		["Reel Technique"] = 88,
		["Angler's Instincts"] = 89,
		["Steed's Blessing"] = 66,
		["Sustaining Shadows"] = 65,
	}
	for skillName, skillID in ipairs(championSkills) do
		if string.find(MyAddon.savedVariables.autoMetSwap, skillName, 1, true) ~= nil then
			return skillID
		end
	end
end
I'm wanting the function to return the skillID into the swapSkill variable at the top but its returning nill nomatter what I do.

Last edited by sinnereso : 05/19/23 at 10:55 AM.
  Reply With Quote
05/19/23, 12:41 PM   #2
Masteroshi430
 
Masteroshi430's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2020
Posts: 185
Originally Posted by sinnereso View Post
hey guys,

Im messing around with tables for a new feature mostly to understand these better and I hit a wall. I keep getting nil as a returned value and I cant see why.

Code:
function MyAddon.FakeFunction()
     MyAddon.savedVariables.autoMetSwap = "Master Gatherer"
     local swapSkill = MyAddon.FindChampionSwapSkill()
     df(tostring(swapSkill))
end

function MyAddon.FindChampionSwapSkill()
	--/script  df(GetChampionSkillName(number))
	local championSkills = {
		["Master Gatherer"] = 78,
		["Treasure Hunter"] = 79,
		["Gifted Rider"] = 92,
		["Homemaker"] = 91,
		["Cutpurse's Art"] = 90,
		["Infamous"] = 77,
		["Reel Technique"] = 88,
		["Angler's Instincts"] = 89,
		["Steed's Blessing"] = 66,
		["Sustaining Shadows"] = 65,
	}
	for skillName, skillID in ipairs(championSkills) do
		if string.find(MyAddon.savedVariables.autoMetSwap, skillName, 1, true) ~= nil then
			return skillID
		end
	end
end
I'm wanting the function to return the skillID into the swapSkill variable at the top but its returning nill nomatter what I do.
df(tostring(swapSkill)) -> d(tostring(swapSkill)) maybe?
  Reply With Quote
05/19/23, 12:56 PM   #3
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 244
nope doesnt work either... I've even put a chat output inside the:

Code:
for skillName, skillID in ipairs(championSkills) do
		df(tostring("HELLO"))
and its not outputting so something wrong with the for statement but its looks perfect to me. In other examples most people use _ dummy for skillName but in my case I want that info. Does it need to be a number or index or can it be strings like I have there?

OR... theres something wrong with how im calling the function with:

Code:
local swapSkill = MyAddon.FindChampionSwapSkill()
im googling now on calling functions but I'm pretty sure this is fine as well

Last edited by sinnereso : 05/19/23 at 01:00 PM.
  Reply With Quote
05/19/23, 01:24 PM   #4
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 244
makes no sense to me.. HELLO outputs fine everytime.. HELLO 2 not even once ever. I even tried reversing the names and ID's like this.

Code:
function MyAddon.FindChampionSwapSkill()
	--/script  df(GetChampionSkillName(number))
	df("HELLO")
	local championSkills = {
		[78] = "Master Gatherer",
		[79] = "Treasure Hunter",
		[92] = "Gifted Rider",
		[91] = "Homemaker",
		[90] = "Cutpurse's Art",
		[77] = "Infamous",
		[88] = "Reel Technique",
		[89] = "Angler's Instincts",
		[66] = "Steed's Blessing",
		[65] = "Sustaining Shadows",
	}
	for skillID, skillName in ipairs(championSkills) do
		df("HELLO 2")
		if string.find(tostring(MyAddon.savedVariables.autoMetSwap), tostring(skillName), 1, true) ~= nil then
			return tonumber(skillID)
		end
	end
end
  Reply With Quote
05/19/23, 03:01 PM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,963
Ipairs does not work with non indexed, non gap table keys like strings are!
Use pairs

Ipairs only works if table is an array like key 1,2,3,4,...without any gap in between
  Reply With Quote
05/19/23, 03:11 PM   #6
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 244
Yeh I think Ive figure that out.

Now im trying to use string.find to find "Someplace" in example("Someplace 88").

and then later to find the "88" and save the 88 to a variable.

* just realized you said use pairs.. ill try it

Last edited by sinnereso : 05/19/23 at 03:14 PM.
  Reply With Quote
05/19/23, 03:51 PM   #7
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 244
Originally Posted by Baertram View Post
Ipairs does not work with non indexed, non gap table keys like strings are!
Use pairs

Ipairs only works if table is an array like key 1,2,3,4,...without any gap in between
OMG ty! that worked perfectly
  Reply With Quote
05/20/23, 06:15 AM   #8
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,963
yw.

btw, why don't you use the IDs of the champion skills instead of the names? Again this will be complicating everything if you try to search and compare strings with multi language games.
You really should rethink your learning curve and check if there are IDs (numbers) to use instead of strings. Once you learned that you never will fallback to strings anymore, and it will be less complicative
  Reply With Quote
05/20/23, 03:35 PM   #9
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 244
YA i do agree with you but I dont feel im ready yet to even consider other languages etc. Thats going to be my final thing down the road.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » tables and string.find help

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off