About Us
Products
Purchase
Downloads
Support
Forums
Contact Us
Site
 Register to post in forums, or Log in to your existing account
 

 Related 
Contents
Using Lua in CMUD
  -1: Introduction
  0: For the Novice
  1: Numbers and Maths
  2: Words
  3: Variables
  4: Simple Functions
  5: Tables
  6: Branching
  7: More Flow Control
  8: Writing Functions
  9: CMUD Examples
  10: Beyond This Guide
Related Links:
  1: Numbers and Maths
2: Words [[cmud_tut_lua_2]] 
When we talk about words in a script we call them strings. Perhaps this is because all the letters are strung together into a group, like those big banners that say "Happy Birthday, Harold!" - and there you have it. A string. Here are some more strings:


"How come Harold gets a banner and I don't?"
"^*£$*! I hit my thumb with this hammer!"
"Which do you prefer, the number 5 or the number 7?"
"           "
""

As you can see, strings can contain any characters at all, even none. The important thing is that they're surrounded by "quotes" - it's the quotes that tell Lua that you're talking about a string and not about something else.

Try using print to display some strings.


print("Hello?")
print("Hello!?")
print("Is anyone out there?")

Hello?
Hello!?
Is anyone out there?

Try some of your own strings.

Arithmetic with Strings

Now then. You can't add strings together (try it - Lua will give you a nice error) but you can join them together. This is called concatenating, which is just a fancy way of saying "sticking to the end of" and in Lua it's represented by two dots - ... Just like 4 plus 3 is 7, "Fang" concat "Xianfu" is "FangXianfu".


print(4+3)
print("Fang".."Xianfu")
print("Fang"+"Xianfu")

7
FangXianfu
error: attempt to perform arithmetic on a string value

You'll notice here that concating things doesn't add spaces. You'll have to do that yourself. To get my name formatted properly, you could do "Fang ".."Xianfu" or perhaps "Fang".." Xianfu" or even "Fang" .. " " .. "Xianfu".

7 or "7"?

Before we go any further, I feel the need to talk about the difference between the number 7 and the string "7". I already showed how Lua doesn't know how to add strings, but Lua's pretty smart. If you try to add strings and the strings will turn into numbers (the string "7" obviously turns into a number, but the string "ham" doesn't) then Lua will do the conversion for you.


print("7"+"7")
print("7"+7)
print(7 ..7)

14
14
77

Notice how I had to put a space between the first 7 and the dots that symbolise the concat. This is because dots are sometimes parts of numbers too (like 7.6) and Lua thinks that the first dot in 7..7 is part of the number. But of course 7..7 isn't a proper number, so Lua will tell you that you've made a malformed number. You might find it easier to get into the habit of always putting spaces between the dots and the things you're concating.

You're probably thinking "So 7 and "7" are different. If Lua changes them for me, what does it matter?". All will become apparent in a few chapters' time. For now, just remember that there's a difference.

Speech in Strings

What if we had a string that involved someone saying something?


print("Sally said, "Bob, you smell of cheese!"")

Doesn't work too well, does it? Lua thinks that the quote before Bob's name is the end of the string, so the words Bob, you smell of cheese! are outside the string.

If you want to use the " character inside strings, you have to tell Lua that you don't want to use its special string-ending property and just want it to be a normal character. You do this by escaping the character.

So to get Sally properly insulting Bob, we do this:


print("Sally said, \"Bob, you smell of cheese!\"")

Sally said, "Bob, you smell of cheese!"

If a character has a backslash before it, Lua will check if the character has special properties and turn them off. The only special characters you really need to worry about for now are the " character and the \ itself. Here are some more examples:


print("\"Well, I do work in a cheese factory!\" Bob retorted")
print("A backslash! \\")
print("snakes\\plane")
print("snakes\plane")

"Well, I do work in a cheese factory!" Bob retorted
A backslash! \
snakes\plane
snakesplane

In that last example, the backslash tried to escape the p. The backslash vanishes, but p doesn't do anything special, so it's left alone.

That's enough playing with letters. On, to Chapter 3!

Tim Toady

If you have experience with other languages, you might be wondering why Lua doesn't just use the addition symbol for concatenation like many other languages do, making "Fang"+"Xianfu" give the result "FangXianfu". If you think about this, though, you'll see a problem. Since Lua will automatically convert numbers into strings and vice versa, it'd make "7"+7 ambiguous. Lua wouldn't know which answer you wanted, 14 or "77". That's why Lua has a separate symbol for concatenation.

There are more special characters in strings, so don't just go putting backslashes anywhere or you'll end up putting in characters that you didn't want, like \t, a tab, or \n, a new line. Most of the special characters aren't really needed, but you might get some mileage out of \n


print("Hello!\nHow are you today?")

Hello!
How are you today?


The Length Operator

Like the + operator for adding and the .. operator for concatenating, Lua has a length operator - #. Quite simply, it gives you the length of what it's used on:


print(#"A string, a lovely string")

25


More String Delimiters

As well as using " to start and end a string, we can also use '.


print("Hello")
print('Hello')

Hello
Hello

This is important because only the character that started the string is a special character inside the string. So instead of quoting out all those speech marks when Sally was insulting Bob, we could just use the other kind of string-starter.


print('Sally said, "Bob, you smell of cheese!"')

Sally said, "Bob, you smell of cheese!"

It's recommended that you only use one kind of quote to open and close a string throughout your script unless your string has that kind of quote in it. But ultimately it's up to you how you write your scripts, so do what you like.

A Short Aside

Here's something really interesting - Lua doesn't care about white space. You can have as many or as few spaces and new lines in your script as you like and Lua doesn't care. These examples are all identical in function:


-- One
print("One string")
print("Two strings")

-- Two
print("One string")print("Two strings")

-- Three
print             (              "One string"            )



print                            (                  "Two strings"                )

so you're free to add as much or as little space between things as you like - the important thing is that you do it consistently and that when you read it back, it makes sense to you. Programmers disagree on what makes code look good, so just do whatever makes you happy.

The single exception to this rule is that the opening bracket for a function must be on the same line as the function name. This doesn't work because it's ambiguous and if you try it, Lua will tell you that:


print
("a string")

error: ambiguous syntax (function call x new statement) near '('
Viewer Comments [0 - Post your comments]

Jump to:  

© 2009 Zugg Software. Hosted by Wolfpaw.net