Jump to content




Some lua tricks you may not have known...


106 replies to this topic

#81 lordwh

  • Members
  • 3 posts
  • LocationHome

Posted 29 June 2013 - 04:27 AM

I am only a beginner and stopped at the middle. It got too intense. And also, NitrogenFingers is my fav Programmer/Youtuber too! He got me back into computer craft, because I liked the RPG engine he made.

#82 Encreedem

  • Members
  • 39 posts
  • LocationVirgo Supercluster

Posted 02 July 2013 - 04:49 AM

I didn't know that these boolean functions exist in LUA too. Thanks, it will definitely help me with my code!

#83 Cutecurtain

  • Members
  • 31 posts
  • LocationBodafors, Sweden

Posted 19 July 2013 - 07:35 AM

I learned quite a few more tricks here! ;) Thanks! B)

#84 CyborgAlex

  • Members
  • 9 posts

Posted 30 July 2013 - 10:37 AM

Some of then i knew,but some realy helped me.Thanks

#85 TimTheRedstoner

  • Members
  • 37 posts

Posted 31 July 2013 - 09:03 PM

very nice will give a +1

#86 NEOparmen

  • Members
  • 17 posts

Posted 09 August 2013 - 04:40 AM

Very good!

#87 Lux

  • Members
  • 19 posts
  • LocationArgentina

Posted 27 August 2013 - 10:36 PM

Oh god! Thanks for post it! I'm learning LUA and this is very useful! Thanks a lot!

#88 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 29 August 2013 - 06:58 PM

I'm bookmarking this thread as I will be coming back to it often to optimize and enhance my code as the case may be. Btw, +1'd you! :)

#89 jay5476

  • Members
  • 289 posts

Posted 03 September 2013 - 03:28 AM

you could add that
[=[ can have --[[comments]] in it ]=]
I just found this out :)

#90 ChemE

  • Members
  • 4 posts

Posted 20 November 2013 - 10:47 AM

Great collection of tips! I always enjoy these when learning a new programming language. Much appreciated!

#91 _gjkf_

  • Members
  • 14 posts

Posted 24 November 2013 - 11:14 AM

great job!!!
thanks a lot!!!

#92 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 24 November 2013 - 02:34 PM

Thanks for the comments, everyone! I've gotten around to rewriting the entire post (since there were some misinformations, bad wordings some important left-out information here and there), and you can access it here: http://kingdaro.gith...lua-tricks.html

Also, it'd be great if someone could inform me on how to have Lua syntax highlighting on here.

Edited by Kingdaro, 24 November 2013 - 02:34 PM.


#93 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 04 December 2013 - 10:42 PM

I couldn't have ever known we can print "Hi" like in old versions (2.7) of Python! As well, I thought you needed to use ]]-- to end a multi-line comment. Thanks and +1 to you!

I think you use ```lua to get a Lua syntax highlight. Confirming now, will edit when confirmed.

Edit: Yep, you use ```lua , like this:
```lua
print("Lua syntax highlighting!!!")
-- Better lua code then that
```
Some GitHub Tricks You Probably Knew :)/>/>

EDIT: The editor derped up my post :)

Edited by awsmazinggenius, 04 December 2013 - 10:48 PM.


#94 distantcam

  • Members
  • 139 posts
  • LocationChunk 0

Posted 04 December 2013 - 11:23 PM

View PostKingdaro, on 20 August 2012 - 09:51 AM, said:

--snip
...indicing tables using strings is actually a little slower than using the dot syntax...
-snip

Does anyone know why this is?

Edited by distantcam, 04 December 2013 - 11:25 PM.


#95 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 December 2013 - 01:46 AM

View Postdistantcam, on 04 December 2013 - 11:23 PM, said:

Does anyone know why this is?
I believe its to do with the way LuaJ implements it.

EDIT: Sorry I should expand on that a bit.
LuaJ maintains 3 immutable arrays for a single table.
  • an array to store indexed values
  • an array to store hash keys
  • an array to store hash values
This means that when getting an element from a table the simple array lookup is quicker than the hash lookup (especially since they don't actually use a HashMap, and use two separate arrays).

It is also slightly slower to add a key/value pair as opposed to an indexed value due to the fact that there are two arrays that need to be modified; hashKey and hashValue arrays. Otherwise it is still fairly expensive to add elements to a table due to the fact that they must create a new array each time, since arrays in Java are immutable. I do think that they really should implement it as mutable data structures, for example an ArrayList and HashMap.

If you're interested in seeing most of the relevant implementation

Edited by theoriginalbit, 05 December 2013 - 02:00 AM.


#96 distantcam

  • Members
  • 139 posts
  • LocationChunk 0

Posted 05 December 2013 - 03:32 AM

While that was interesting (I didn't know about how Lua tables worked under the covers) it didn't really answer my question.

Why is
table["foo"]
slower than
table.foo
?

#97 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 December 2013 - 04:51 AM

An educated guess based on the extract TheOriginalBIT posted is that when using the dot, "hashget()" is called - whereas when using the brackets, rawget() is called.

The latter function checks to see if what you put in the square brackets was a number or not, then if not, it just calls the former function and returns whatever that returns. Obviously it's faster to skip that extra check and call by rigging things so the former function is called directly.

It also appears that adding elements to a table does not require a new array each time - rather, if the current array is full then a new array double the size of the last is built, meaning after each resize it takes longer and longer until the next one is needed.

#98 digpoe

  • Members
  • 92 posts

Posted 17 May 2015 - 04:37 PM

View Postdistantcam, on 05 December 2013 - 03:32 AM, said:

While that was interesting (I didn't know about how Lua tables worked under the covers) it didn't really answer my question.

Why is
table["foo"]
slower than
table.foo
?

This is also actually a thing in the Lua VM. It's because there's an entire opcode dedicated to accessing tables using dots.
An excerpt from http://lua-users.org...ationCodingTips;
Spoiler

Edited by digpoe, 17 May 2015 - 04:39 PM.


#99 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 18 May 2015 - 05:53 PM

View Postdigpoe, on 17 May 2015 - 04:37 PM, said:

This is also actually a thing in the Lua VM. It's because there's an entire opcode dedicated to accessing tables using dots.
An excerpt from http://lua-users.org...ationCodingTips;
Spoiler

*ahem, necro*

GET/SETDOTTED aren't present in Lua 5.1. They were in Lua 4. If there really is a difference, it probably would be from the Lua compiler emitting code which loads the string into a register before doing GET/SETTABLE rather than just referencing a constant index in the GET/SETTABLE instruction.

a.b = 3
settable <register of var a> <const index to string "b"> <const index to number 3>

a["b"] = 3
loadk <some register r> <const index to string "b">
settable <register of var a> <r> <const index to number 3>

The register argument probably triggers the check for correct type like BombBloke was saying. The constant argument doesn't require this since it is guaranteed to be whatever its type is in the constant pool. Even if that's not the case, loading the constant into a register takes an extra instruction anyways.

#100 Thefdjurt

  • Members
  • 45 posts
  • LocationMy house

Posted 26 May 2015 - 07:22 PM

Cool stuffs (even though I knew it all ;)), but the link is dead.
GitHub's current page system requires you to create a new project then create a branch named "gh-pages"-- I recommend setting this as the default branch-- , and finally then add an "index.html" file.

The squid knows all....

Edited by Thefdjurt, 27 May 2015 - 05:11 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users