PtokaX forum

Development Section => Your Developing Problems => Topic started by: Hydrogen on 21 October, 2004, 20:06:42

Title: Outputting index in a table
Post by: Hydrogen on 21 October, 2004, 20:06:42
Sorry if this has been covered before, I searched and found no results - maybe I searched for the wrong item.. any help with this would be much appreciated.

If have a table such as:

table = { user1 = 1, user2 = 1, user3 = 1, user4 = 2, user5 = 1 }

I wish to output in the hub any user with the value of "1"
for instance:
Table = user1, user2, user3, user5

i've tried a number of things and cannot get it to work right, I am fairly new to Lua, but learning fast, again any help with this would be much appreciated.    

Hydrogen
Title:
Post by: bastya_elvtars on 21 October, 2004, 21:34:58
table = { [user1] = 1, [user2] = 1, [user3] = 1, [user4] = 2, [user5] = 1 }
this is theway how indexing of tables works.
Title:
Post by: Hydrogen on 21 October, 2004, 21:45:54
Yes but I wish to output any thing in the table with a value of "1",  

ie:
I want the script to be able to output this into the hub:
 Table = user1, user2, user3, user5
Title:
Post by: bastya_elvtars on 21 October, 2004, 21:52:32
for index,value in table do -- do this till there are indexes & values in the table
   if value=1 then -- if value is 1
   SendToAll(value) -- sends tehe value into main
   end
end

thats the general sketch
Title:
Post by: Herodes on 21 October, 2004, 22:16:37
Just another show-how
local finalString = ""
for index,value in table do -- do this till there are indexes & values in the table
if value=1 then -- if value is 1
finalString = finalString..index..", "
end
end
SendToAll(finalString) -- sends tehe value into main
Title:
Post by: Hydrogen on 21 October, 2004, 22:27:27
I appreciate your help very much :)
but wouldnt it go like this:
---------------------------------------------
for i,v in table do
     if v == 1 then
          SendToAll(i)
     end
end
---------------------------------------------

Now all I need to figure out is how to make it one line, it is currently displaying:

user1
user2
user3
user5

I would like it to display:
user1, user2, user3, user5

Thank you for shedding some light on the subject for me... Im halfway there now
Title:
Post by: Hydrogen on 21 October, 2004, 22:34:02
thank herodes for your reply .. although with this code:

function printtable(user,data)
   local finalstring = ""
   for i,v in test do
      if v == 1 then
         finalstring = finalstring.. i..", "
         SendToAll(finalstring)
      end
   end
end

it displays this in main:
user1,
user1, user2,
user1, user2, user3,
user1, user2, user3, user5

how would I go about fixing this?
Title:
Post by: bastya_elvtars on 21 October, 2004, 22:39:29
function printtable(user,data)
 local finalstring
 for i,v in test do
  if v == 1 then
   finalstring = finalstring.. i..", "
  end
 end
  SendToAll(finalstring)
end
Title:
Post by: Hydrogen on 21 October, 2004, 22:47:47
thank you bastya and herodes,
got it working now...
cheers