Outputting index in a table
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Outputting index in a table

Started by Hydrogen, 21 October, 2004, 20:06:42

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Hydrogen

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

bastya_elvtars

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.

Hydrogen

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

bastya_elvtars

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

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

Hydrogen

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

Hydrogen

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?

bastya_elvtars

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.

Hydrogen

thank you bastya and herodes,
got it working now...
cheers

SMF spam blocked by CleanTalk