Role of "_" in for loops?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Role of "_" in for loops?

Post by onedaysnotice »

Hi, I'm kinda learning lua as I program my game, so theres many parts of the language I'm still unsure of. So to the point, whilst studying people's codes I've noticed numerous times that "_" (without quotes) have been used instead of i. Is there a difference between just using i, and underscore? If so, what is its purpose?

An example of what I'm referring to:

for _,v in ipairs() do
-etc
end

thanks :D
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Role of "_" in for loops?

Post by dreadkillz »

I think its to indicate that its a non important variable when you're reading your code. At least that's what I use it for.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Role of "_" in for loops?

Post by kikito »

It's a readability thing.

The code will "work" both ways - with a k or with _. But with the underscore you make clear that "you are not going to use this variable at all".

Another typical example is when you have a function that returns several values. For example, string.find returns the position of the first character and the last character of a pattern found in a string. If you are going to use only the "end position", and don't care about the start, you can do this:

Code: Select all

local _, endPos = string.find("Where does Lua end?", "Lua")
print("Lua ends in the " .. tostring(endPos) .. " character")
When I write def I mean function.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: Role of "_" in for loops?

Post by onedaysnotice »

Ok thanks heaps guys :D
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Role of "_" in for loops?

Post by josefnpat »

From the Lua Style Guide: http://lua-users.org/wiki/LuaStyleGuide

The variable consisting of only an underscore "_" is commonly used as a placeholder when you want to ignore the variable:

for _,v in ipairs(t) do print(v) end
Note: This resembles the use of "_" in Haskell, Erlang, Ocaml, and Prolog languages, where "_" takes the special meaning of anonymous (ignored) variables in pattern matches. In Lua, "_" is only a convention with no inherent special meaning though. Semantic editors that normally flag unused variables may avoid doing so for variables named "_" (e.g. LuaInspect is such a case).
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Role of "_" in for loops?

Post by Jasoco »

It's just a throwaway. It signifies that it's not important and won't be used, but is there because something needs to be there. So instead of using say, "i" or "n", just use the underscore as it's easier to work around in case you do need to use i or n later.
Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests