Jump to content




What does the underscore mean in a for loop?

help

  • You cannot reply to this topic
7 replies to this topic

#1 diamondpumpkin

  • Members
  • 59 posts
  • LocationPlaces, 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 by diamondpumpkin, 15 March 2016 - 08:09 PM.


#2 Dragon53535

  • Members
  • 973 posts
  • LocationIn 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.

#3 diamondpumpkin

  • Members
  • 59 posts
  • LocationPlaces, Ukraine

Posted 15 March 2016 - 08:20 PM

View PostDragon53535, on 15 March 2016 - 08:10 PM, said:

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?

#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn 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.

#5 diamondpumpkin

  • Members
  • 59 posts
  • LocationPlaces, Ukraine

Posted 15 March 2016 - 08:24 PM

Oh, okay. That makes sense.

#6 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 15 March 2016 - 08:53 PM

View PostDragon53535, on 15 March 2016 - 08:10 PM, said:

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.

#7 Dragon53535

  • Members
  • 973 posts
  • LocationIn 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.

#8 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users