Jump to content




# opperator

lua

11 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 17 March 2016 - 04:00 PM

How does work the # operator?
I know, that I can preload APIs or something like that, but how does it work?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 March 2016 - 04:32 PM

# translates roughly to "get length of"

It works on strings and tables.

#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 17 March 2016 - 04:37 PM

For tables the length (#) operator counts numerical indexes of a table, starting from the index 1 until it hits a nil value. If you're using a table as an array it will work as a simple array length operator. For strings the length operator counts the number of bytes (characters) of the string. You could write it (for tables) as a function in Lua like so:

local function length (tab)
  local len, i = 0, 1

  while tab[i] ~= nil do
    len = len + 1
    i = i + 1
  end

  return len
end

local t = {
 1, 2, 3
}

print(#t) --> 3

print(length(t)) --> 3

EDIT: got ninja'd and forgot about strings.

Edited by MKlegoman357, 17 March 2016 - 04:41 PM.


#4 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 March 2016 - 06:42 PM

I think OP confused it with another language. cant remember which though. does anybody know of such preloading of libraries?

#5 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 17 March 2016 - 06:46 PM

View PostInDieTasten, on 17 March 2016 - 06:42 PM, said:

I think OP confused it with another language. cant remember which though. does anybody know of such preloading of libraries?

Might be pre-processor directives. They are used in C and C++, as well as many other languages. There are some pre-processors written for CC. Sewbacca, are you talking about these:

#include <string>

#define FOO 1
#define BAR

#ifdef BAR

#endif

Edited by MKlegoman357, 17 March 2016 - 06:47 PM.


#6 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 17 March 2016 - 07:01 PM

View PostMKlegoman357, on 17 March 2016 - 06:46 PM, said:

View PostInDieTasten, on 17 March 2016 - 06:42 PM, said:

I think OP confused it with another language. cant remember which though. does anybody know of such preloading of libraries?

Might be pre-processor directives. They are used in C and C++, as well as many other languages. There are some pre-processors written for CC. Sewbacca, are you talking about these:

#include <string>

#define FOO 1
#define BAR

#ifdef BAR

#endif

Yes, I mean that, but it doesn't work.

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 March 2016 - 07:01 PM

It doesn't work because that isn't Lua.

#8 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 17 March 2016 - 07:21 PM

Here
Maybe, I don't understand it or it doesn't work in CC.

#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 March 2016 - 07:41 PM

Why would the command to run a lua program on Unix be valid lua code?

#10 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 17 March 2016 - 08:06 PM

I thought, that you can load an API before executing the code, so I didn't understand it :(.

#11 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 March 2016 - 08:49 PM

What you are referring to are shell default application headings for files. In order to run a lua script, you would need to pass its file as argument to the lua interpreter executable.
Which is kind of annoying, even more so when not set in the current PATH.

What Lua does allow is to create this shell line in the first line of the file, which is actually ignored by the interpreter, when it starts with #!
Following is the path of the default application to open it with. This is also used in normal shellscripts. Lua is just compatible with that style, telling the interpreter of the file via this line. It allows you to handle your script much like a program, rather than a text file.

Edit: The reason it doesn't work in CC is probably because the java lua interpreter doesnt support ignoring this line.

In CC you dont even need it though. Typing a filename into the shell automatically executes it via dofile. so interpreter must be set, since everything is already in a lua environment

Edited by InDieTasten, 17 March 2016 - 08:53 PM.


#12 Lignum

  • Members
  • 558 posts

Posted 17 March 2016 - 10:31 PM

View PostInDieTasten, on 17 March 2016 - 08:49 PM, said:

...

For the sake of completeness, it's called a Shebang.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users