This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

# opperator

Started by Sewbacca, 17 March 2016 - 03:00 PM
Sewbacca #1
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?
KingofGamesYami #2
Posted 17 March 2016 - 04:32 PM
# translates roughly to "get length of"

It works on strings and tables.
MKlegoman357 #3
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 on 17 March 2016 - 03:41 PM
InDieTasten #4
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?
MKlegoman357 #5
Posted 17 March 2016 - 06:46 PM
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 on 17 March 2016 - 05:47 PM
Sewbacca #6
Posted 17 March 2016 - 07:01 PM
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.
KingofGamesYami #7
Posted 17 March 2016 - 07:01 PM
It doesn't work because that isn't Lua.
Sewbacca #8
Posted 17 March 2016 - 07:21 PM
Here
Maybe, I don't understand it or it doesn't work in CC.
KingofGamesYami #9
Posted 17 March 2016 - 07:41 PM
Why would the command to run a lua program on Unix be valid lua code?
Sewbacca #10
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 :(/>.
InDieTasten #11
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 on 17 March 2016 - 07:53 PM
Lignum #12
Posted 17 March 2016 - 10:31 PM

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