Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What does ~= do in Lua?... See it everywhere but still confused on what it does

Asked by 5 years ago

Hello, I have a question. What does this do? ~=

Looks like an arithmetic operator, but not sure.

I've seen it used like this before.

local exampleVarriable = "Example"

if exampleVarriable ~= nil then
print("Something...")
end

4 answers

Log in to vote
4
Answered by 5 years ago
Edited 5 years ago

The ~= symbol means not equal

So basically saying

if exampleVarriable is not equal to nil then print("something...")

0
I see, similar to !== in other OOP languages? andyad13 74 — 5y
1
It's the same as != in C++. Troyonix 5 — 5y
0
!== is used in php AlphaGamer150 101 — 5y
0
No, !== means not equal and not same type, like == means equals, but === means equals and type. So you would probably just do != in php unless you needed to check the type of a variable. SebbyTheGODKid 198 — 5y
View all comments (3 more)
0
Your right! User404_no13 0 — 5y
0
Most languages don't have `===` and `!==` operators. C, C++, Java, C#, neither have it. `==` already behaves like that. Link150 1355 — 5y
0
Typo. I meant != andyad13 74 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

~= means not equal. For example, 1 ~= 2

If both numbers(or strings, or tables, etc.) are not equal to each other, it will return true. If both are equal, it will return false.

Log in to vote
0
Answered by
Link150 1355 Badge of Merit Moderation Voter
5 years ago
Edited 5 years ago

~= is Lua's inequality comparison operator.

a ~= b will evaluate to true if a and b are not equal. However if either a or b have a metatable with an __eq field set to a function then Lua will call that function instead. To avoid calling this function and perform a raw comparison, Lua provides a function call rawequal which compares a and b without invoking the __eq metamethod.

This last bit of information is really only of use to you if you make use of metatables, but I found it interesting to bring up anyway.

An interesting thing to note about equality comparisons is that {} == {} will never be true, as tables are compared by reference and not by value. This means that a variable holding a table will only ever be equal to another if the second refers to the same table in memory:

local t1 = {}
local t2 = {}

print(t1 == t2) -- false

t3 = t1

print(t1 == t3) -- true

Yet another thing to note is that NaN -- a special number constant meaning "Not a Number" and returned by undefined math operations -- equals nothing, not even itself.

local x = 0/0

print(x ~= x) -- true
Log in to vote
-1
Answered by
D3LTA_Y 72
5 years ago

"==" ---> Equal to "~=" ---> Different

Answer this question