59 posts
Location
Places, Ukraine
Posted 15 March 2016 - 08:07 PM
I'm still new to loops, and I've recently come across a piece of code that is helpful but I don't entirely get it.
Code:
local f = fs.list("disk")
for _, file in ipairs(f) do
print(file)
end
What does the underscore mean in the loop?
Edited on 15 March 2016 - 07:09 PM
1080 posts
Location
In the Matrix
Posted 15 March 2016 - 08:10 PM
It's a variable name given by the programmer to signify that they don't really care about what's assigned to that variable.
In this case _ is the index of the current element 'file', since this program is looping through file names it doesn't care about the order in which they come through which is why it's an underscore there.
59 posts
Location
Places, Ukraine
Posted 15 March 2016 - 08:20 PM
It's a variable name given by the programmer to signify that they don't really care about what's assigned to that variable.
In this case _ is the index of the current element 'file', since this program is looping through file names it doesn't care about the order in which they come through which is why it's an underscore there.
So could I use the underscore for an unknown amount of times?
1080 posts
Location
In the Matrix
Posted 15 March 2016 - 08:22 PM
I'm not sure what you mean. The underscore is just a variable name, that's it. It's no different from the file variable or the f variable used in that script. It's just given the underscore to signify that the programmer doesn't care about what is assigned to that variable.
59 posts
Location
Places, Ukraine
Posted 15 March 2016 - 08:24 PM
Oh, okay. That makes sense.
2427 posts
Location
UK
Posted 15 March 2016 - 08:53 PM
It's a variable name given by the programmer to signify that they don't really care about what's assigned to that variable.
Actually to Lua it's just an ordinary variable name, it's convention which says that the contents of the variable is something that we are not interested in.
1080 posts
Location
In the Matrix
Posted 15 March 2016 - 08:57 PM
Is that not what I said? It was a name given by the programmer of that script which conventionally means that they don't care at all what happens to that variable because they're not going to use it.
2427 posts
Location
UK
Posted 15 March 2016 - 09:41 PM
That was a big ninja'd, that is why you read and answer each topic one at a time and try to not get interrupted while doing it.
The post I Quoted was the one I was clarifying, but you did that before I clicked post (but after I had opened the thread).