Ripull
|
  |
| Joined: 21 Jul 2008 |
| Total Posts: 14249 |
|
|
| 18 Jul 2014 05:49 PM |
I was messing around trying stuff out and I can't seem to get this to print anything:
list = {["1"] = {"a"}, ["2"] = {"b"}, ["3"] = {"c"}, ["4"] = {"d"}}
for i = 1, #list do print(list[tostring(i)]) end
--It should print 4 tables, but prints nothing. Changing list[tostring(i)] to list[i] doesn't work also.
Anyone know what I'm doing wrong? |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2014 05:52 PM |
list = {["1"] = {"a"}, ["2"] = {"b"}, ["3"] = {"c"}, ["4"] = {"d"}}
for i,v in next, list do print(list[i][1]) end |
|
|
| Report Abuse |
|
|
Ripull
|
  |
| Joined: 21 Jul 2008 |
| Total Posts: 14249 |
|
| |
|
|
| 18 Jul 2014 06:05 PM |
| If you ever cycle through a table it is best to use pairs, ipairs, or next. I personally prefer using next, but all three of those are better than doing "for i = 1, table do." |
|
|
| Report Abuse |
|
|
sbk28
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 2528 |
|
|
| 18 Jul 2014 06:06 PM |
if you just wanted your version fixed it would be
print(tostring(list[i])) |
|
|
| Report Abuse |
|
|
sbk28
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 2528 |
|
|
| 18 Jul 2014 06:07 PM |
nvm
read the question as table and assumed it was one instead of a dictionary, ignore me |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2014 06:07 PM |
| @sbk - Did you test that? Because that doesn't work. |
|
|
| Report Abuse |
|
|
| |
|
Ripull
|
  |
| Joined: 21 Jul 2008 |
| Total Posts: 14249 |
|
|
| 18 Jul 2014 06:17 PM |
Actually, not this doesn't work.
I needed it in the format of:
list = {["1"] = {"a"}, ["2"] = {"b"}, ["3"] = {"c"}, ["4"] = {"d"}}
for i = #list,1,-1 do print(list[i]) end
..basically going from the last to the first value, but this still doesn't work. |
|
|
| Report Abuse |
|
|
sbk28
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 2528 |
|
|
| 18 Jul 2014 06:18 PM |
It's because your "list" is actually what we would define as a dictionary with number strings for keys, this gives it an index of zero, so you're effectively saying
for i = 1, 0 do
i.e. your loop isnt running |
|
|
| Report Abuse |
|
|
sbk28
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 2528 |
|
|
| 18 Jul 2014 06:20 PM |
why not just
list = {{"a"}; {"b"}; {"c"}; {"d"}}
for i = #list, 1, -1 do print(list[i][1]) end |
|
|
| Report Abuse |
|
|
Ripull
|
  |
| Joined: 21 Jul 2008 |
| Total Posts: 14249 |
|
|
| 18 Jul 2014 06:21 PM |
See I need it to be in this format to avoid having to loop through an absolutely massive lua table every time I want to compare a value (what I posted here is just a super simplified example of what I'm doing.)
I need to keep the dot notation ability. |
|
|
| Report Abuse |
|
|
sbk28
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 2528 |
|
|
| 18 Jul 2014 06:22 PM |
| There's no way of doing that with a dictionary format without having a preset length due to the fact a dictionary won't return an index on #table |
|
|
| Report Abuse |
|
|
Ripull
|
  |
| Joined: 21 Jul 2008 |
| Total Posts: 14249 |
|
| |
|
Ripull
|
  |
| Joined: 21 Jul 2008 |
| Total Posts: 14249 |
|
|
| 18 Jul 2014 06:28 PM |
| Why does list[tostring(i)] not work? Because I would have thought referencing the index to a string would opened me up to quick reference to the table. |
|
|
| Report Abuse |
|
|
sbk28
|
  |
| Joined: 15 Nov 2008 |
| Total Posts: 2528 |
|
|
| 18 Jul 2014 06:30 PM |
because your list is a dictionary
so you can have
list = {["1"] = {"a"}, ["2"] = {"b"}, ["3"] = {"c"}, ["4"] = {"d"}}
#list == 0
because it's not indexed, your values are "keys" not indexes, and keys store values
so your for loop isnt actually running due to a 0 index |
|
|
| Report Abuse |
|
|