Working with Strings and Arrays (Tables)

So I'm just getting started with Corona, and I've found that the explanations of strings and tables in the documentation is not really spelled out in a way that I can understand as a flash developer.

First off I'll start with what I figured out on my own after a bit of looking at other projects.

Adding two strings together is very different in Corona:

While this works in AS3 and outputs "My string is: My string text."

1
2
var myString:String = "My string text.";
trace("My string is: "+myString);

I will try to help you:

1
2
3
4
5
6
7
8
9
--create a table
myTable = {}
 
--create an object in the table
myTable[1] = "string text"
 
--print table length
 
print(#myTable)

Ok so I'm assuming that tables don't start with 0 like in actionscript. And #myTable will give the actual length of the table.

Now if I wanted to loop through the contents of the table I would set the for statement with #myTable or whatever to get the proper length for the loop?

I'm sorry I'm not actually trying this stuff out yet, but it's on my mind that's for sure.

1
2
3
4
local i
for i = 1, #myTable do
   print(myTable[i])
end

A word of caution...

Getting an element from the table would require square brackets '[' and ']'

However, for ease of understanding, Arrays and Dictionaries are the two types of 'Tables' and # would return the length of an 'Array' type table not a 'dictionary' type table.

Arrays are not 0 based, but 1 based, Dictionary object that is not applicable.

cheers,

?:)

views:1914 update:2011/10/10 15:46:04
corona forums © 2003-2011