It is currently April 24th, 2024, 12:45 pm

[HELP] Lua "and"

Discuss the use of Lua in Script measures.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

[HELP] Lua "and"

Post by smurfier »

The tutorial isn't actually being very helpful on how to use "and". This is what I'm failing at:

Code: Select all

   if Length<=Width and Mode=2 then
      Int = 0
   else
      Int = 1
   end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua "and"

Post by jsmorley »

It isn't the "and" that is giving you trouble here, it is the fact that you are using an assignment operator "=" when you mean to use a comparison operator "==" in "Mode=2".
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Help with Lua "and"

Post by Yggdrasil »

A shorter&faster way to use these kind of codes:

Code: Select all

Int = (Length<=Width and Mode==2) and 0 or 1
:)
Image
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Help with Lua "and"

Post by smurfier »

Tee hee... Rainmeter has me spoiled.

Also... Didn't know I could do it that way. I thought that in lua they evaluate to true or false.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua "and"

Post by jsmorley »

Yggdrasil wrote:A shorter&faster way to use these kind of codes:

Code: Select all

Int = (Length<=Width and Mode==2) and 0 or 1
:)
Right. Lua "shorthand" stuff is very cool, but to be honest I'm glad it also works in a more "basic" syntax structure to initially be a bit more accessible for new users. Lets someone who isn't a programmer wrap their head around it a bit quicker. I do suggest that anyone who wants to really get better at this Lua stuff take a look at the "shortcuts" that Lua has. They are clever indeed. They can reduce code, and although in the little scripts we are writing for Rainmeter a few cycles either way is not going to generally make any difference, they can be more efficient.
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Help with Lua "and"

Post by Yggdrasil »

In conditions, nil and false is false, everything else is true (so 0 is true) By the way, the result of a comparison is always a boolean (true/false)
Image
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Help with Lua "and"

Post by smurfier »

Could you please explain the shorthand a bit, I haven't gotten that far into my understanding yet. I'm a basic scripter as most of my experience lies with writing basic for my TI-89 back in high school.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua "and"

Post by jsmorley »

See what I meant in my PM smurfie? :-)
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua "and"

Post by jsmorley »

smurfier wrote:Could you please explain the shorthand a bit, I haven't gotten that far into my understanding yet. I'm a basic scripter as most of my experience lies with writing basic for my TI-89 back in high school.
I'm no expert, I'm just starting to wrap my head around a lot of it myself, but this page has some good info on how "expressions" can be efficiently written.

http://lua-users.org/wiki/ExpressionsTutorial
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Help with Lua "and"

Post by smurfier »

I understand your PM, I just wanted to hide my "duh" moment behind closed doors.

I just need to understand that particular like of shorthand because I feel really bad "publishing" a line of code that I don't understand. Also, I've been reading the tutorial, I'm just one that needs to learn things the hard way.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .