Author Topic: Outputting index in a table  (Read 2516 times)

0 Members and 1 Guest are viewing this topic.

Offline Hydrogen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
Outputting index in a table
« 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

PtokaX forum

Outputting index in a table
« on: 21 October, 2004, 20:06:42 »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #1 on: 21 October, 2004, 21:34:58 »
Code: [Select]
table = { [user1] = 1, [user2] = 1, [user3] = 1, [user4] = 2, [user5] = 1 }
this is theway how indexing of tables works.
Everything could have been anything else and it would have just as much meaning.

Offline Hydrogen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
(No subject)
« Reply #2 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

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #3 on: 21 October, 2004, 21:52:32 »
Code: [Select]
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
Everything could have been anything else and it would have just as much meaning.

Herodes

  • Guest
(No subject)
« Reply #4 on: 21 October, 2004, 22:16:37 »
Just another show-how
Code: [Select]
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

Offline Hydrogen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
(No subject)
« Reply #5 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

Offline Hydrogen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
(No subject)
« Reply #6 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?

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 752
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
(No subject)
« Reply #7 on: 21 October, 2004, 22:39:29 »
Code: [Select]
function printtable(user,data)
 local finalstring
 for i,v in test do
  if v == 1 then
   finalstring = finalstring.. i..", "
  end
 end
  SendToAll(finalstring)
end
Everything could have been anything else and it would have just as much meaning.

Offline Hydrogen

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
(No subject)
« Reply #8 on: 21 October, 2004, 22:47:47 »
thank you bastya and herodes,
got it working now...
cheers

PtokaX forum

(No subject)
« Reply #8 on: 21 October, 2004, 22:47:47 »