|
| 22 Feb 2012 08:22 PM |
I have an array, lets say that it's myArray={"A","B","C"}
How do I find the key of item "B" ? |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 22 Feb 2012 08:25 PM |
You mean "table"?
myArray[2] |
|
|
| Report Abuse |
|
|
lordyoman
|
  |
| Joined: 10 Jun 2010 |
| Total Posts: 426 |
|
| |
|
|
| 22 Feb 2012 08:27 PM |
Arrays and tables are the same thing.
I mean, how do I find that "B" is number 2? |
|
|
| Report Abuse |
|
|
|
| 22 Feb 2012 08:28 PM |
| If you go to (almost) any other language (Even VB) it's called an Array |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 22 Feb 2012 08:31 PM |
Well, it's called a table in Lua, so just remember that to prevent confusion. Anyway, you can do that with a loop:
table = {"A", "B", "C"}
for i, v in pairs(table) do if (v == "B") then print(i) end end
Will print "2" |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 22 Feb 2012 09:00 PM |
At blockoo
Its an array. A table is an empty dictionary, an array is a dictionary with elements. Matrix is a dictionary with multiple arrays? |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 22 Feb 2012 09:35 PM |
| No, they're interchangeable terms for the most part. Think about a real-life table. Even if you put things on it, you still call it a table, right? |
|
|
| Report Abuse |
|
|
|
| 22 Feb 2012 09:45 PM |
OMG PEOPLE! Table is the Lua term for it, but MOST people call it an array...It DOESN'T MATTER which of the two INTERCHANGEABLE terms is used. As for your question, you'd do this:
arrayName[2] |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 22 Feb 2012 09:49 PM |
> No, they're interchangeable terms for the most part. Think about a real-life table. Even if you put things on it, you still call it a table, right?
Wrong type of table broski. Look up mathematical tables.
Also an array is a multi-dimensional array, not a table with mutliple arrays. |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 22 Feb 2012 09:50 PM |
@bballer Your explanation of table vs. array is correct, but that's not what he asked for. He wanted to be able to get the index number of "B". |
|
|
| Report Abuse |
|
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
|
| 22 Feb 2012 09:51 PM |
"Also an array is a multi-dimensional array"
...what? |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
| |
|
blockoo
|
  |
| Joined: 08 Nov 2007 |
| Total Posts: 17202 |
|
| |
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 22 Feb 2012 10:02 PM |
| The most common response is that a table is empty whereas an array has elements. Personally I always thought the difference was that arrays have infinite dimensions/indices and can hold any type of data value. Whereas tables have a defined size and data-type ( `int[] tab=new int[5];` ) however it really shouldn't matter. |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2012 12:07 AM |
| Well, that's annoying. I always thought of an array as a table with only numeric indices. To the Wiki! Yep, I'm right >=D |
|
|
| Report Abuse |
|
|
|
| 23 Feb 2012 12:28 AM |
@merlin11188
You are right and I was actually going to point it out.
Arrays and tables aren't the same thing. Arrays are values ordered in a certain order, while Lua tables are data structures that consist of a pair of keys and values, both the key and the value being able to be any kind of value except nil. |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 23 Feb 2012 02:50 AM |
Almost forgot :P Tables are not aren't even a data structure http://www.lua.org/pil/11.html |
|
|
| Report Abuse |
|
|