A string-counting question (I think)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

A string-counting question (I think)

Post by stout »

Code: Select all

function audienceStateCheck()
   audienceDiscontent = 0
   audienceNeutral = 0
   audienceContent = 0

	for i = 1,#audience do
		if audience[i].mood == "content" then
			audienceContent = audienceContent + 1
		elseif audience[i].mood == "neutral" then
			audienceNeutral = audienceNeutral + 1
		elseif audience[i].mood == "discontent" then
			audienceDiscontent = audienceDiscontent + 1
		end
	end
end

personGuyOne = { name = "Guy One", age = 52, gender = male, likes = {"glad", "happy"}, dislikes = {"sunday"}, mood = "discontent" }
personGalTwo = { name = "Gal Two", age = 42, gender = female, likes = {"dancing", "map", "history"}, dislikes = {"vibrant"}, mood = "content" }

audience = {personGuyOne, personGalTwo}
So what I have are a lot of nested tables, each table is a person, each person has a mood. I want to count all of the "contents" and put those in a variable; likewise "discontent" and "neutral". And these tallies need to be updated constantly; thus the variables declared in the function, so they get reset every update. Right now it's being called into a function that gets called in love.draw() so I can print some values.

And this loop works! Yay for me. But it feels clunky and I'm not sure whether it'll give me bugs later on down the road. It seems like there should be a simpler/better way to say, "Add up all the instances of thing X within table Y". Something with the string. library?
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: A string-counting question (I think)

Post by qaisjp »

Code: Select all

function audienceStateCheck()
   audienceDiscontent = 0
   audienceNeutral = 0
   audienceContent = 0

	for i,v in ipairs(audience) do
                local mood = v.mood
                local var = "audience"..string.upper(mood:sub(1,1))..mood:sub(2,#str)
                _G[var] = _G[var] + 1
	end
end

personGuyOne = { name = "Guy One", age = 52, gender = male, likes = {"glad", "happy"}, dislikes = {"sunday"}, mood = "discontent" }
personGalTwo = { name = "Gal Two", age = 42, gender = female, likes = {"dancing", "map", "history"}, dislikes = {"vibrant"}, mood = "content" }

audience = {personGuyOne, personGalTwo}
var becomes "audienceDiscontent", "audienceContent", "audienceNeutral". if the mood was set to "test" then it would increment "audienceTest". it will cause errors if the audienceVariable variable is not a number.

remember:
http://pgl.yoyo.org/luai/i/ipairs
pairs is the same as ipairs except it works with all indexes
Lua is not an acronym.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: A string-counting question (I think)

Post by stout »

Can you walk me through a couple of things on this? I haven't done anything with : or _G yet.

Code: Select all

local var = "audience"..string.upper(mood:sub(1,1))..mood:sub(2,#str)
I get what's happening here; it gets the mood value - say, "content" - and then capitalizes the first letter and appends it to "audience" so it'll get "audienceContent" and then be able to modify that variable, now that it has a name. But I don't understand the "sub" function - all I can find is maybe string.sub? But why can you call it with mood: if that's the case? Also I figure #str is getting the rest of the string but str isn't initialized anywhere, so how is that working?

Also, why do you need to use _G, why not just the variable? It's defined within the function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A string-counting question (I think)

Post by Robin »

That's damn ugly. That code should be written as:

Code: Select all

function audienceStateCheck()
	local audienceState = {content = 0, discontent = 0, neutral = 0}
	for i,v in ipairs(audience) do
                audienceState[v.mood] = audience[v.mood] + 1
	end
	return audienceState
end

personGuyOne = { name = "Guy One", age = 52, gender = male, likes = {"glad", "happy"}, dislikes = {"sunday"}, mood = "discontent" }
personGalTwo = { name = "Gal Two", age = 42, gender = female, likes = {"dancing", "map", "history"}, dislikes = {"vibrant"}, mood = "content" }

audience = {personGuyOne, personGalTwo}
Help us help you: attach a .love.
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: A string-counting question (I think)

Post by qaisjp »

stout wrote:Can you walk me through a couple of things on this? I haven't done anything with : or _G yet.

Code: Select all

local var = "audience"..string.upper(mood:sub(1,1))..mood:sub(2,#str)
I get what's happening here; it gets the mood value - say, "content" - and then capitalizes the first letter and appends it to "audience" so it'll get "audienceContent" and then be able to modify that variable, now that it has a name. But I don't understand the "sub" function - all I can find is maybe string.sub? But why can you call it with mood: if that's the case? Also I figure #str is getting the rest of the string but str isn't initialized anywhere, so how is that working?

Also, why do you need to use _G, why not just the variable? It's defined within the function.
Use robin's code - but to explain.

I mean #mood (and not #str).
#var will return the length of the variable (if table, amount of indexed items. if string then length of string, if other then ERROR)

for strings, instead of string.sub(var, ...) you can do var:sub(...)
_G is the global list - every global variable is in there. _G._G._G._G["_G"] == _G (not locals)
LOCALS FTW.
Lua is not an acronym.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A string-counting question (I think)

Post by Robin »

Oh, by the way: all strings are usable as tables, in which case they act like the "string" table. So ("foo"):upper() really means string.upper("foo").
Help us help you: attach a .love.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: A string-counting question (I think)

Post by stout »

Thanks! I don't know why I didn't think about putting the tabulated variables into a table. It was originally a numeric for because... I.. can't remember why. -.- Slowly learning...
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 199 guests