Love understanding pluralization?

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
thiama
Prole
Posts: 3
Joined: Sat Jan 28, 2017 12:28 am

Love understanding pluralization?

Post 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?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Love understanding pluralization?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Love understanding pluralization?

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
thiama
Prole
Posts: 3
Joined: Sat Jan 28, 2017 12:28 am

Re: Love understanding pluralization?

Post 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!
Post Reply

Who is online

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