Page 1 of 1

Love understanding pluralization?

Posted: Sun Mar 19, 2017 5:09 pm
by thiama
I'm a beginner and am working on reading files right now after finishing my first few test projects. I've not had trouble understanding anything so far but this one I can't seem to understand.

Why does love accept "for k, file"? Where is file defined? Does love understand what a file is? Is file an internal pointer command?
Where can I find information of special words like these that are so generic they pop up everywhere within the documentation which makes them really hard to search for? What do you even call them?

Code: Select all

local dir = ""
--assuming that our path is full of lovely files (it should at least contain main.lua in this case)
local files = love.filesystem.getDirectoryItems(dir)
for k, file in ipairs(files) do
	print(k .. ". " .. file) --outputs something like "1. main.lua"
end
Another example if this code that I wrote which makes slightly more sense to me

Code: Select all

for line in files:lines() do
	name = string.match(line, '%a+')
	number = tonumber(string.match(line,'%d+'))
	numbers[d] = {activity, timeofactivity}
end
What am I missing here? How does love know that the table files contains "file"s?

Re: Love understanding pluralization?

Posted: Sun Mar 19, 2017 5:28 pm
by zorg

Code: Select all

local files = love.filesystem.getDirectoryItems(dir)
that function returns a table, containing files and directories in the directory "dir" you gave it as a parameter.

Code: Select all

for k, file in ipairs(files) do
This is lua's generic iterator syntax:
for <comma delimited list of variables returned by the iterator> in <iterator function> do
Or specifically, for ipairs:
for <name of local current key in loop>, <name of local current value in loop> in <iterator function> do

Whatever the iterator returns each cycle will be put in those two variables, here being called "k" and "file"; because ipairs is used, k will be a numeric index, and file will be the value in the files table at the index k (or in other words, file = files[k]).

Code: Select all

for line in files:lines() do
Another generic for loop with a different iterator; this time it only returns one value, and the variable used is called "line", which holds the contents of each line in the "files" variable... though i suspect that should be a File object, since :lines is defined on those, only.

Re: Love understanding pluralization?

Posted: Sun Mar 19, 2017 5:30 pm
by s-ol
thiama wrote: Sun Mar 19, 2017 5:09 pm I'm a beginner and am working on reading files right now after finishing my first few test projects. I've not had trouble understanding anything so far but this one I can't seem to understand.

Why does love accept "for k, file"? Where is file defined? Does love understand what a file is? Is file an internal pointer command?
Where can I find information of special words like these that are so generic they pop up everywhere within the documentation which makes them really hard to search for? What do you even call them?

Code: Select all

local dir = ""
--assuming that our path is full of lovely files (it should at least contain main.lua in this case)
local files = love.filesystem.getDirectoryItems(dir)
for k, file in ipairs(files) do
	print(k .. ". " .. file) --outputs something like "1. main.lua"
end
Another example if this code that I wrote which makes slightly more sense to me

Code: Select all

for line in files:lines() do
	name = string.match(line, '%a+')
	number = tonumber(string.match(line,'%d+'))
	numbers[d] = {activity, timeofactivity}
end
What am I missing here? How does love know that the table files contains "file"s?
got ninja'd, but with references:

it does not at all. 'file' is a variable name and you can pick any variable name you want.
if you change 'file' to 'spam' and it will work the same.

Also this is not love specific, and therefore covered in the lua documentation:
https://www.lua.org/pil/4.3.4.html
https://www.lua.org/pil/4.3.5.html

if you didn't already and find you have trouble with Lua in other cases aswell, you should probably read at least parts one and two of Programming In Lua.

Re: Love understanding pluralization?

Posted: Sun Mar 19, 2017 7:45 pm
by thiama
Oh you guys are the best. I can't begin to say how grateful I am. This clears up all my confusion.

Thank you very much Zorg. And thank you very much s-ol!