For Loop Through Values of a table

Hello

I am trying to use a for loop to iterate a list of values inside a table element. I can access these individually by printing:

print(Data[1]["Cue"])

which will print the value of “Cue” Inside Element 1 of the Table (Data). In this case of the pic below this will print “0.1”.

However I’d love to loop through these value rather than just access them individually. For example, there are multiple values to each element in the table and the number of values can change. Below pic shows an example:

Any tips or hints please let me know

Thanks

Elliot

You’re going to need a for loop or nested for loops for this. Nested for loops can handle nested lists but besides that a simple for loop will iterate over all of the items in your list.

for index, value in pairs(Data) do 
	print(index, value)
end
3 Likes
for i, v in pairs (data) do
      print(v)
end

This should work? Let me know if it doesn’t it’s 1 am I’m not thinking fluently

Ok thanks for the heads up. So what I am dealing with is a nested list. Good to know.

To handle a nested list:

for i, v in pairs(List) do
 -- v will be the lists that are each item in i
  for j, k in pairs(v) do
    -- k will be the items in each individual list j
    CodeHere
  end
end
2 Likes

Hi Tyler148,

Thanks for the really useful starting point. I’m definitely on the right lines.
However, I can’t seem to get a return using this method. I’m sure I’m slipping up somewhere really easily.

Below is what I’m trying to do with a bit of explanation of my thoughts for context.

--*DEFINE CHANNEL INDEX*
local ChanIndex = {}

for i, v in ipairs (Data[1]) do -- this is the list as shown in the previous photo.
	
	for j, k in ipairs (v) do
		print (j,k) -- In theory, I should see (for example): ["01"], "0" - printed for each entry in the list.
		table.insert(ChanIndex,(tonumber(j)) ) -- adds the number of values to a table.
	end
end

Can you see where I might be slipping up?

Elliot

Since you said your lists were nested, I gave example code to handle a dictionary with values which are lists, like this:

local List = {
	["01"] = {"0", "2", "3",},
	
	["02"] = {"4","5","6",}
}
for i, v in pairs(List) do
	for j, k in pairs(v) do
		print(k)
	end
end

After looking at your list in the original post, this just looks like a dictionary. What values are you trying to insert into the table exactly, and how does it work? I’m having trouble understanding what exactly you need to do.

This may help? This code takes a dictionary structured similarly to the one you provided in the OP, inserts the values associated with each key into a separate table, then prints them.

local List = {
	["01"] = "0",
	["02"] = "1"
}
local TableToInsertInto = {}

for i, v in pairs(List) do
	table.insert(TableToInsertInto, tonumber(v))
end

for j, k in pairs(TableToInsertInto) do
	print(tostring(k))
end

Thanks Tyler!

So a little bit about what I’m doing, Standby for some nerdy details:

In a nutshell - this is a simple simulation of a Theatre Stage Lighting Console which I am recreating inside of Roblox.

This information is coming from my Google Sheets document via an API which is fetched via the HTTPService. The Blue column represents the Cue Number and the Green Row represents each Individual Channel for Stage Lights. Pic for clarity:

In JSON (on the web) the Data is displayed like this in Rows:

This Data is fetched via the HTTPService and comes in and is stored as a variable (Data). Each Row is stored as an Indexed Value (as you would find in a table). Within the Indexed Value, a nested dictionary is stored as shown in the above post (the print to output).

So

print(Data[1]) -- will Print Row 1 of this Data.
print(Data[5]) -- will Print Row 5 of this Data.
print(Data[3]["FOLLOW"] -- will Print the Value of the 'Follow' Column in Row 3.

This is Great - I’m really pleased with how far I’ve come with this and I’m happy with the fact I can access this information over the internet in quick succession. However, I am designing my google sheet to be expandable based on the amount of Channels (or stage lights) I want to virtually rig in studio. Therefore within Roblox I want to be able to count the number of entries of the dictionary within each Indexed Value to then further manipulate this in Roblox.

I hope that makes some sort of sense? I will try your latest suggestions and feed back.

Thanks very much for sticking with me on this one you are humbly appreciated!

Apologies for all the Rambling On…

Elliot

Very cool implementation you’re working towards here!

Okay so, it looks like the JSON is structured as an Array where each array position is a dictionary.

Try this if you could, and see if it prints the value and key of each dictionary entry:

for i=1, #Data do
  for j, k in pairs(Data[i]) do
      print(j, k)
  end
end

This is a learning experience for me too so sorry if this isn’t a rote solution. Anything using HTTPService is exciting though so I’ll be happy when we get it sorted out!

Heya,

Thanks will have a crack and feed back on the progress. It’s all a bit of a learning experience for me too to be honest! Once it’s working more than happy to share with you / show you the ropes!