generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Syntax problem?

Previous Thread :: Next Thread 
Ripull is not online. 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
LUVMARIOLUIGI10000 is not online. LUVMARIOLUIGI10000
Joined: 16 May 2009
Total Posts: 1244
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 is not online. Ripull
Joined: 21 Jul 2008
Total Posts: 14249
18 Jul 2014 05:59 PM
Perfect.
Report Abuse
LUVMARIOLUIGI10000 is not online. LUVMARIOLUIGI10000
Joined: 16 May 2009
Total Posts: 1244
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 is not online. 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 is not online. 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
LUVMARIOLUIGI10000 is not online. LUVMARIOLUIGI10000
Joined: 16 May 2009
Total Posts: 1244
18 Jul 2014 06:07 PM
@sbk - Did you test that? Because that doesn't work.
Report Abuse
LUVMARIOLUIGI10000 is not online. LUVMARIOLUIGI10000
Joined: 16 May 2009
Total Posts: 1244
18 Jul 2014 06:08 PM
ninja'd qq
Report Abuse
Ripull is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. Ripull
Joined: 21 Jul 2008
Total Posts: 14249
18 Jul 2014 06:23 PM
lol i hate lua
Report Abuse
Ripull is not online. 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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image