PtokaX forum

Development Section => Your Developing Problems => Topic started by: Optimus on 11 December, 2003, 15:26:50

Title: How to sort this Table??
Post by: Optimus on 11 December, 2003, 15:26:50
Hi guy's i'm trying to sort this Table, but without any succes.

if (cmd == "!show") then
         n=1
         local disp = ""
         disp = disp.."\t\tNr:\tTime:\tNick:\r\n"
         for i,v in UserHubTime do
            local T = UserHubTime["Total"]
               disp = disp.."\t\t"..n..".\t"..T.." min\t"..i.."\r\n"
               n = n + 1
            end
               user:SendData(sBot, disp)
            end
         end


Thx allready L8trs
Title:
Post by: c h i l l a on 11 December, 2003, 15:36:41
Buy,  entering all your table entries in a non assocciative table, like this


test = {}

for i, v un UserHUbTime do
  tinsert(test,{ i,v["Total"] }
end

sort(test, function(a,b) return (a[2] > b[2]) end)

or something like that...  in one of my elder scripts i also found this...  but not usefull actually..  but it works


--max is the maximum number of shown entries

function GetUserMaxTime()
TCopy={}
for i,v in UserHubTime do
TCopy[i]=v
end
local msgfromtxt ="\r\n"
for i = 1,Max do
var1 = 0
for index, value in TCopy do
value = tonumber (value)
if value > var1 then
var1 = value
userm = index
end
end
local tmp = TCopy[userm]
local months, days, hours, minutes = floor(tmp/43200), floor(mod(tmp/1440, 30)), floor(mod(tmp/60, 24)), floor(mod(tmp/1, 60))
TCopy[userm]=0
msgfromtxt = msgfromtxt.."          "..i.." - "..userm.." :: "..months.." Months, "..days.." Days, "..hours.." Hours, "..minutes.." Min ( "..tmp.." min ).\r\n"
end
return msgfromtxt
end

Title:
Post by: Optimus on 11 December, 2003, 15:45:28
there seems to be something wrong with this Line:

tinsert(test,{i,v["Total"]}
Title:
Post by: c h i l l a on 11 December, 2003, 16:08:42
try

tinsert(test,{i,UserHubTime.Total})

or tinsert(test,{i,UserHubTime["Total"]})

else just try around..  or just
send me a copy of the table of how it looks like
Title:
Post by: Guibs on 11 December, 2003, 16:12:16
Hi there,,

Optimus,.. i'm not sure to get what you wish,.... but,
maybe try:
---
for i,v in UserHubTime do
local T = UserHubTime
disp = disp.."\t\t"..n..".\t"..T.." Total min\t"..i.."\r\n"
n = n + 1
end
---
If i get your idea,,,... :)

l8tr,, ;)
Title:
Post by: Optimus on 11 December, 2003, 16:12:35
kk Chilla i will send you a copy in PM.

And thx Guibs for having a look also... Structure is just abit more complex :D
Title:
Post by: Guibs on 11 December, 2003, 16:31:46
lol,,

ok,... :o)
Title:
Post by: Optimus on 11 December, 2003, 16:34:57
Ok solved now by Chilla... Big Thanks

Code:

function GetUserMaxTime()
   TCopy={}
   for i,v in UserHubTime do
      tinsert(TCopy,{i,v.Total})
   end
   sort(TCopy,function(a,b) return (a[2] > b[2]) end)
   local msgfromtxt ="\r\n"
   for i = 1,Max do
      if TCopy then
         msgfromtxt = msgfromtxt..""..i.." - "..TCopy[1].." :: "..TCopy[2].."\r\n"
      end
   end
   return msgfromtxt
end
Title:
Post by: Guibs on 11 December, 2003, 16:41:23
looks very nice, yep,...
Thks to both of you to have post this syntax,... :)

l8tr,, ;)
Title:
Post by: Optimus on 11 December, 2003, 16:48:33
Hihi, no prob Guibs m8

Btw, this was the reason for the Sorting Structure...
Having a abnormal Table *lol*

{{
["[SU]Guibs"]={2},
["[SU]Me"]={3},
},
{
["Session"]=56,
["Total"]=101,
},
{
["Session"]=34,
["Total"]=86,
},
}
Title:
Post by: Guibs on 11 December, 2003, 18:43:11
h? h? h?
yup, i get your '["Total"]' problem,, now,.. ;)