0

I've been trying to get me a array like this:

["Some String"] = true,
["Some other string"] = true
etc

by using code.

I havn't got any clue on how to create a array. i've tried:

local tempArray
tempArray = {}
tempArray["Some String"] = true

but this doesn't work, it sais tempArray = nil.

What am I doing wrong?

1
  • 3
    Are you trying to run this interactively? local will isolate that variable to that chunk (line) of input. But in your example, tempArray would then be defined global on the next line. Commented Sep 22, 2011 at 13:01

3 Answers 3

4

There's nothing wrong at all in the code you've posted.

Sign up to request clarification or add additional context in comments.

2 Comments

strange that when I test it the array remains nil.
@Levisaxos: Then the problem is in the test.
1

I've always created arrays in Lua like:

local myArray = {
  ["Hello"]    = 'World',
  ["Testing"]  = '123'
}

That should work, if it doesn't make sure your Lua installation is up to date and working correctly.

3 Comments

ofcourse static arrays are made like that. But mine has to be dynamical.
How are you testing your arrays?
It's part of a world of warcraft addon. Using local when having it save to variables makes it nil all the time.
0

There is, of course nothing wrong in the code you posted. However, it might act oddly in an interactive environment, depending on how chunks are collected and handed to the parser.

As written, you declare a local tempArray and then apparently use it. If those lines are typed into the interactive Lua prompt which takes each line as an individual chunk, then the local created in the first line will be created and discarded. The second line will create a global variable with the same name, and the third line will use the global to set a field. You can demonstrate this like this:

C:\Users\Ross>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> local tempArray
> tempArray = {}
> tempArray["Some String"] = true
>
> table.foreach(tempArray,print)
Some String     true
>
> print(type(_G.tempArray))
table
>

Here, I've demonstrated that the table tempArray exists and has exactly the one key with the value true. By printing the type of _G.tempArray I've demonstrated that a global variable was created.

By using a do ... end pair to force the interactive prompt to treat the whole block as a single chunk, we both create and use the local variable. Unfortunately after the end of the chunk, the local is now out of scope and can't be seen any more.

C:\Users\Ross>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> do
>> local tempArray
>> tempArray = {}
>> tempArray["Some String"] = true
>> end
> print(type(_G.tempArray))
nil
> table.foreach(tempArray,print)
stdin:1: bad argument #1 to 'foreach' (table expected, got nil)
stack traceback:
        [C]: in function 'foreach'
        stdin:1: in main chunk
        [C]: ?
>

I don't know enough about WoW to speak with authority, but it is likely that locals declared in a script may have interesting issues with visibility and value persistence. If they use a significant amount of sandboxing of scripts, even globals in a script may not be visible to other scripts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.