Jump to content

Multiple values for a string key in table


Mega9

Recommended Posts

I was wondering how do I add more values to it since I need to loop the table and put the values of the right matching key in a gridlist, but when I use comma to separate more values it doesn't work and adds only the first value in gridlist.

  
local exampleTable = { 
    stringKey = "Value 1" 
} 
  

Link to comment

Sorry guys, I explained my problem really sloppy, problem was actually turning those:

  
local exampleTable = { 
    stringKey = "Value 1", 
    secondString = "Value 2" 
} 
  

into:

  
local exampleTable = { 
    stringKey = "Value 1", "Value 2", "Value 3", 
    secondStringKey = "Mike", "Micheal" 
} 
  

So basically, 1 key = more than 1 value/string. And then when I make it the way I just did, it loops the table, but adds only first string/value of every string key. Do you know what I mean? Sorry I haven't explained it properly in the first post.

Link to comment

No problem at all then, try this:

local exampleTable = { 
    [stringKey1] = { "Value 1", "value 2", "value 3" }, 
    [stringKey2] = { "Value 1a", "value 2a", "value 3a" }, 
    [stringKey3] = { "Value 1b", "value 2b", "value 3b" } 
} 
  

Multidimensional table if that's what you asked for ;)

Link to comment
  • Moderators

@mrbrutus1467 and all who don't understand....

local exampleTable = {

[stringKey1] = { "Value 1", "value 2", "value 3" },

}

Now stringKey1 have to be a variable, which isn't defined.

It is:

  
local exampleTable = { 
    stringKey1 = { "Value 1", "value 2", "value 3" } 
} 
  

or

  
local exampleTable = { 
    ["stringKey1"] = { "Value 1", "value 2", "value 3" } 
} 

or with the variable stringKey1 but defined.

  
local stringKey1 = "stringKey1" 
  
local exampleTable = { 
    [stringKey1] = { "Value 1", "value 2", "value 3" } 
} 

Link to comment

@IIYAMA Unlike you I expected mega9 to be smart enough to realize the fact that a undefined variable points at nil and therefore it can't be indexed. The original question was about multidimensional arrays or tables, that has nothing to do with what you can use in order to index each row, the rows doesn't even need an index either, there are many ways to solve this kind of issue in, but just because everyone doesn't use your solution doesn't mean they are idiots.

@mega9 You do realize that all you did was to create a bad implemented table, index it to nil, and then change it to a regular one dimensional array like you had in the first place. [ and ] should not be used in one dimensional arrays only in multidimensional ones like in my first example. If you want a sample that works out of the box then you should specify the details in the description.

Link to comment
  • Moderators
@IIYAMA Unlike you I expected mega9 to be smart enough to realize the fact that a undefined variable points at nil and therefore it can't be indexed. The original question was about multidimensional arrays or tables, that has nothing to do with what you can use in order to index each row, the rows doesn't even need an index either, there are many ways to solve this kind of issue in, but just because everyone doesn't use your solution doesn't mean they are idiots.

@mega9 You do realize that all you did was to create a bad implemented table, index it to nil, and then change it to a regular one dimensional array like you had in the first place. [ and ] should not be used in one dimensional arrays only in multidimensional ones like in my first example. If you want a sample that works out of the box then you should specify the details in the description.

Well mister, 8 of the 10 people that use this forum(scripting section) don't understand a damn about it.

Because they learn lua via mta and not via lua sites(like: http://lua-users.org/)

There isn't enough information there to know what ways there are.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...