[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Sorting Tables and Spitting out Data
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Sorting Tables and Spitting out Data

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Strazor   USA  (24 posts)  [Biography] bio
Date Thu 30 Oct 2014 09:24 PM (UTC)

Amended on Thu 30 Oct 2014 09:27 PM (UTC) by Strazor

Message
I have a table called 'tdata' with the following information.

"LIGHT"=0
"SONIC"=-92
"NEGATIVE"=0
"HOLY"=0
"SLASH"=0
"MENTAL"=0
"EARTH"=-80
"MAGIC"=0
"AIR"=0
"FIRE"=0
"ELECTRIC"=-300
"WATER"=248
"ACID"=0
"SHADOW"=0
"BASH"=0
"PIERCE"=0
"POISON"=0
"COLD"=136
"ENERGY"=0
"DISEASE"=0


I use the following to sort the data ascending.


t = {}

for k, v in pairs (tdata) do
      table.insert (t, k) 
  end -- for

table.sort (t, function (c, d) return tdata [c] < tdata [d] end)
table.foreach (t, print)


This above sorts the table correctly and outputs the following in the correct Ascending order:


1 ELECTRIC
2 SONIC
3 EARTH
4 LIGHT
5 ACID
6 SHADOW
7 BASH
8 POISON
9 PIERCE
10 ENERGY
11 FIRE
12 DISEASE
13 AIR
14 NEGATIVE
15 SLASH
16 HOLY
17 MAGIC
18 MENTAL
19 COLD
20 WATER


Here is where I'm having trouble visualizing the next step. I want to be able to do two things.
1) I want the value of each damage type listed. (ie, '2 SONIC -92' from above data)
2) I want to be able to ColourNote each line based on a value. For instance, if value is < 0 make the text green. Using the above data it would make '2 Sonic -92' a green color.

Any suggestions ?

Much Thanks for reading.
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #1 on Thu 30 Oct 2014 10:08 PM (UTC)
Message
I'd probably go with an if check nested in your sort routine.

Something like

  for k, v in pairs (tdata) do
      if k < 0 
          table.insert ColorNote(t, k)
       else
          table.insert (t, k)
       end -- if
  end -- for


Guarantee the colornote syntax is off but you get the general isea

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Strazor   USA  (24 posts)  [Biography] bio
Date Reply #2 on Thu 30 Oct 2014 10:42 PM (UTC)

Amended on Fri 31 Oct 2014 12:21 AM (UTC) by Strazor

Message
That is a great idea for printing the data. I still have the issue of the sorted table not listing all the data.

Your first idea worked great:


for k, v in pairs (tdata) do
      if v <= 0 then
          ColourNote ("#bbbbbb", "white", k,
                      "#bbbbbb", "white", "  ",
                      "#bbbbbb", "white", v)
      elseif
         v > 0 then
                      ColourNote ("#bbbbbb", "red", k,
                      "#bbbbbb", "red", "  ",
                      "#bbbbbb", "red", v)
          ColourNote ("#bbbbbb", "red", k)
      end
end

OUTPUT - You can't see the colors but they are there

LIGHT  0
SONIC  -92
NEGATIVE  0
HOLY  0
SLASH  0
MENTAL  0
EARTH  -80
MAGIC  0
AIR  0
FIRE  0
ELECTRIC  -300
WATER  248
ACID  0
SHADOW  0
BASH  0
PIERCE  0
POISON  0
COLD  136
ENERGY  0
DISEASE  0



What I need now is the sorted table to print correctly. How can I display the values from tdata in t (referenced in my first post)? I want to take the above data and do this.


1 ELECTRIC -300
2 SONIC -92
3 EARTH -80
4 LIGHT 0
5 ACID 0
6 SHADOW 0
7 BASH 0
8 POISON 0
9 PIERCE 0
10 ENERGY 0
11 FIRE 0 
12 DISEASE 0
13 AIR 0 
14 NEGATIVE 0
15 SLASH 0
16 HOLY 0
17 MAGIC 0
18 MENTAL 0
19 COLD 136
20 WATER 248
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #3 on Fri 31 Oct 2014 02:11 AM (UTC)
Message
Offhand, I don't even see what's keeping it from printing both values properly but you didn't provide all of the code for us to see if there's an issue with assigning the values for printing. I offered a suggestion for the problem I might actually have known something useful about :)

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Strazor   USA  (24 posts)  [Biography] bio
Date Reply #4 on Fri 31 Oct 2014 02:21 AM (UTC)

Amended on Fri 31 Oct 2014 02:26 AM (UTC) by Strazor

Message
I'm not sure what else I can add. I've included the table data from tdata. I then provided the code that is used to sort 'tdata' into table 't'. I've shown the code and the results from table 't' but it does not print the attribute data listed in 'tdata'.

In table tdate you get.

"LIGHT"=0
"SONIC"=-92
"NEGATIVE"=0
and so on

Then sorted in table 't'
1 SONIC 
2 LIGHT
3 NEGATIVE
and so on


All of the code I am using is listed in my posts above. I'm trying to figure out how to get the following output from the sorted table 't'.

1 SONIC -92
2 LIGHT  0
3 NEGATIVE  0


Thanks for any help.
[Go to top] top

Posted by Strazor   USA  (24 posts)  [Biography] bio
Date Reply #5 on Fri 31 Oct 2014 02:56 AM (UTC)

Amended on Fri 31 Oct 2014 03:47 AM (UTC) by Strazor

Message
The problem may be how I'm listing the data. I'm going to reorder my table this way and see if the data lists correctly.


tdata = {
    { type = bash, resist = 0 },
    { type = pierce, resist = 5 }
    }

table.sort (tdata, function (k1, k2) return k1.resist < k2.resist end )

table.foreachi (tdata, function (k, v) table.foreach (v, print) end )


How would I update bash and resist in tdata from a variable called by a trigger?


Function Example(name, line, wildcards)
     varType = wildcards[1]
     varResist = tonumber(wildcards[2])
     tdata.type = varType
     tdata.resist = varResist
end -- end function



Would the above function successfully update a table with multiple variables like this?
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Fri 31 Oct 2014 04:12 AM (UTC)

Amended on Fri 31 Oct 2014 04:13 AM (UTC) by Nick Gammon

Message
How about this?


tdata = {
        LIGHT=0,
        SONIC=-92,
        NEGATIVE=0,
        HOLY=0,
        SLASH=0,
        MENTAL=0,
        EARTH=-80,
        MAGIC=0,
        AIR=0,
        FIRE=0,
        ELECTRIC=-300,
        WATER=248,
        ACID=0,
        SHADOW=0,
        BASH=0,
        PIERCE=0,
        POISON=0,
        COLD=136,
        ENERGY=0,
        DISEASE=0,
        }

tsorted = {}

for k, v in pairs (tdata) do
      table.insert (tsorted , k) 
  end -- for

table.sort (tsorted)  -- defaults to ascending

-- read sorted table
for k, v in ipairs (tsorted) do
  val = tdata [v]  -- get value from other table
  if val < 0 then
    col = "green"
  elseif val < 100 then
    col = "magenta"
  else
    col = "lightblue"
  end -- if
  ColourNote (col, "", v .. " = " .. val)
end -- for


Output:


ACID = 0
AIR = 0
BASH = 0
COLD = 136
DISEASE = 0
EARTH = -80
ELECTRIC = -300
ENERGY = 0
FIRE = 0
HOLY = 0
LIGHT = 0
MAGIC = 0
MENTAL = 0
NEGATIVE = 0
PIERCE = 0
POISON = 0
SHADOW = 0
SLASH = 0
SONIC = -92
WATER = 248


Coloured too.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Strazor   USA  (24 posts)  [Biography] bio
Date Reply #7 on Fri 31 Oct 2014 04:17 AM (UTC)
Message
Wow Nick that's great. Can the data be sorted ascending by the values?
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Fri 31 Oct 2014 04:47 AM (UTC)
Message
Sure, like this:


tdata = {
        LIGHT=0,
        SONIC=-92,
        NEGATIVE=0,
        HOLY=0,
        SLASH=0,
        MENTAL=0,
        EARTH=-80,
        MAGIC=0,
        AIR=0,
        FIRE=0,
        ELECTRIC=-300,
        WATER=248,
        ACID=0,
        SHADOW=0,
        BASH=0,
        PIERCE=0,
        POISON=0,
        COLD=136,
        ENERGY=0,
        DISEASE=0,
        }

tsorted = {}

for k, v in pairs (tdata) do
      table.insert (tsorted , k) 
  end -- for

table.sort (tsorted, function (a, b) return tdata [a] < tdata [b] end)
-- read sorted table
for k, v in ipairs (tsorted) do
  val = tdata [v]  -- get value from other table
  if val < 0 then
    col = "green"
  elseif val < 100 then
    col = "magenta"
  else
    col = "lightblue"
  end -- if
  ColourNote (col, "", v .. " = " .. val)
end -- for


Output:


ELECTRIC = -300
SONIC = -92
EARTH = -80
SLASH = 0
SHADOW = 0
ENERGY = 0
BASH = 0
PIERCE = 0
MAGIC = 0
DISEASE = 0
MENTAL = 0
FIRE = 0
HOLY = 0
LIGHT = 0
NEGATIVE = 0
POISON = 0
AIR = 0
ACID = 0
COLD = 136
WATER = 248

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Fri 31 Oct 2014 04:50 AM (UTC)

Amended on Fri 31 Oct 2014 08:26 PM (UTC) by Nick Gammon

Message
Although it's probably better to sort into values first and then if the value is the same, sort alphabetically:


tdata = {
        LIGHT=0,
        SONIC=-92,
        NEGATIVE=0,
        HOLY=0,
        SLASH=0,
        MENTAL=0,
        EARTH=-80,
        MAGIC=0,
        AIR=0,
        FIRE=0,
        ELECTRIC=-300,
        WATER=248,
        ACID=0,
        SHADOW=0,
        BASH=0,
        PIERCE=0,
        POISON=0,
        COLD=136,
        ENERGY=0,
        DISEASE=0,
        }

tsorted = {}


-- k = key, v = value
for k, v in pairs (tdata) do
      table.insert (tsorted , k) 
  end -- for

table.sort (tsorted, function (a, b) 
   if tdata [a] == tdata [b] then
     return a < b
   end -- if values the same
   return tdata [a] < tdata [b] 
   end)

-- read sorted table
-- k = key, v = value
for k, v in ipairs (tsorted) do
  val = tdata [v]  -- get value from other table
  if val < 0 then
    col = "green"
  elseif val < 100 then
    col = "magenta"
  else
    col = "lightblue"
  end -- if
  ColourNote (col, "", v .. " = " .. val)
end -- for


Output:



ELECTRIC = -300
SONIC = -92
EARTH = -80
ACID = 0
AIR = 0
BASH = 0
DISEASE = 0
ENERGY = 0
FIRE = 0
HOLY = 0
LIGHT = 0
MAGIC = 0
MENTAL = 0
NEGATIVE = 0
PIERCE = 0
POISON = 0
SHADOW = 0
SLASH = 0
COLD = 136
WATER = 248

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Fri 31 Oct 2014 04:53 AM (UTC)

Amended on Fri 31 Oct 2014 05:04 AM (UTC) by Nick Gammon

Message


ELECTRIC = -300
SONIC = -92
EARTH = -80
ACID = 0
AIR = 0
BASH = 0
DISEASE = 0
ENERGY = 0
FIRE = 0
HOLY = 0
LIGHT = 0
MAGIC = 0
MENTAL = 0
NEGATIVE = 0
PIERCE = 0
POISON = 0
SHADOW = 0
SLASH = 0
COLD = 136
WATER = 248

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Strazor   USA  (24 posts)  [Biography] bio
Date Reply #11 on Fri 31 Oct 2014 02:03 PM (UTC)
Message
Thank you Nick. Very helpful and much appreciated.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,131 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]