Alexander
Alexander

Reputation: 278

defining a table key with a variable

In PHP I can define a table/array like this:

$string = 'foo';

$array = array();
$array[$string] = 'bar';
print_r($array);         // Result: Array ( [foo] => bar )

so I used the $string to define the key of the first array entry.

How can I accomplish the same in lua?

Edit:

And can I access the result the like this?

print($array[$string]);  // Result: bar

Upvotes: 0

Views: 267

Answers (1)

fiskeben
fiskeben

Reputation: 3467

Like this:

myArray = {}
someString = "foo"
myArray[someString] = "bar"

Read more about tables in Lua here.

Upvotes: 1

Related Questions