rockbandcheeseman

Table Library

Jul 19th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.46 KB | None | 0 0
  1. -- Table Library
  2.  
  3. --[[
  4.     Documentation
  5.    
  6.     This is my extention of the Lua tables library.  I hope you find them all useful.
  7.    
  8.     The following examples will all be interrelated and will create a very basic stats table.
  9.    
  10.     Here are descriptions of each function and how to use them:
  11.    
  12.     table.save(t, filename)
  13.    
  14.         -- t: <table> The table you'll be saving into a textfile.
  15.         -- filename: <string> The name of the file you'll be saving this table to.
  16.        
  17.         Example:
  18.        
  19.             function OnScriptUnload()
  20.            
  21.                 table.save(players, "players.data")
  22.             end
  23.            
  24.         Notes:
  25.        
  26.             -- By default, table.save saves your table in "Documents\\My Games\\Halo\\data".
  27.            
  28.     table.load(filename)
  29.    
  30.         -- filename: <string> The name of the file you want to load a table from.
  31.        
  32.         Example:
  33.        
  34.             function OnScriptLoad(process, game, persistent)
  35.            
  36.                 players = table.load("players.data")
  37.             end
  38.        
  39.             function OnPlayerJoin(player)
  40.            
  41.                 makestats(player)
  42.             end
  43.            
  44.             function makestats(player)
  45.            
  46.                 local hash = gethash(player)
  47.                 stats[hash] = table.load(hash .. ".data")
  48.                
  49.                 players[hash] = players[hash] or {}
  50.                 players[hash].joins = players[hash].joins or 0 + 1
  51.                
  52.                 stats[hash].name = stats[hash].name or getname(player)
  53.                
  54.                 stats[hash].kills = stats[hash].kills or {}
  55.                 stats[hash].kills.humanweap = stats[hash].kills.humanweap or 0
  56.                 stats[hash].kills.covenantweap = stats[hash].kills.covenantweap or 0
  57.             end
  58.        
  59.         Notes:
  60.        
  61.             -- If the file doesn't exist or is empty, table.load returns an empty table {}.
  62.            
  63.     table.len(t)
  64.    
  65.         -- t: <table> Table you want to find the length of.
  66.        
  67.         Returns:  Total length of table.
  68.        
  69.         Example:
  70.        
  71.             local total_players = table.len(players)
  72.            
  73.         Notes:
  74.        
  75.             -- The reason this function is necessary is because #t returns the length of a table as defined by ipairs.  This means Lua begins at index 1 of table t and adds 1 to the index until the value at the current index is nil.  If you have a table that is indexed by hashes, #players will return 0 because players[1] = nil.  table.len returns the length of a table including non-numeric keys.
  76.            
  77.     table.find(t, v, [case])
  78.    
  79.         -- t: <table> Table you're searching.
  80.         -- v: <any type> Value you're searching for in the table.
  81.         -- case: <boolean> Defines if the value should be case-sensitive (default = true).
  82.        
  83.         Returns:  The key at which the specified value is found.
  84.        
  85.         Example:
  86.        
  87.             swears = {"balls", "boners", "poppycock"}  -- My name is Nuggets and I (dis)approve of these words.
  88.            
  89.             function OnServerChat(player, type, message)
  90.            
  91.                 if player then
  92.                     local words = tokenizestring(message)
  93.                     for k,v in ipairs(words) do
  94.                         if table.find(swears, v, false) then  -- ensures that "boners" as well as "BONERS" and "bOnErS" are all blocked (poor boners).
  95.                             privatesay(player, "YOU HAVE SAID EVIL THINGS.")
  96.                             return false
  97.                         end
  98.                     end
  99.                 end
  100.             end
  101.            
  102.     table.max(t)
  103.    
  104.         -- t: <table> Table you want to find the maximum value of.
  105.        
  106.         Returns:  Key which contains the maximum value as well as the maximum value.
  107.        
  108.         Example:
  109.        
  110.             function OnPlayerKill(killer, victim, mode)
  111.            
  112.                 local khash = gethash(killer)
  113.                 if humanweapon(killer) then  -- making up a function here to get to the point
  114.                     stats[khash].kills.humanweap = stats[khash].kills.humanweap + 1
  115.                 end
  116.                
  117.                 local weaptype, kills = table.max(stats[khash].kills)
  118.                 privatesay(killer, "You have the most kills with " .. weaptype .. ": " .. kills)
  119.             end
  120.            
  121.             >> You have the most kills with humanweap: 12
  122.            
  123.     table.maxv(t)
  124.    
  125.         -- See table.max; the only difference is this function only returns the maximum value, not the key at which it was found.
  126.        
  127.     table.maxes(t)
  128.    
  129.         -- t: <table> Table of which you would like to find the keys which have the maximum values.
  130.        
  131.         Returns:  A table of keys which all contain the maximum value of the table and the maximum value.
  132.        
  133.         Example:
  134.        
  135.             local t = {1, 2, 2, 5, 7, 2, 7, 3, 7, 7}
  136.             local keys, max = table.maxes(t)
  137.            
  138.             for k,v in ipairs(keys) do
  139.                 hprintf(v)
  140.             end
  141.            
  142.             hprintf("Max: " .. max)
  143.            
  144.             >> 5
  145.             >> 7
  146.             >> 9
  147.             >> 10
  148.             >> Max: 7
  149.            
  150.     table.sum(t)
  151.    
  152.         -- t: <table> Table of which you would like to find the sum of all numerical values.
  153.        
  154.         Returns:  Sum of all numerical values of the table specified and all tables nested within the table specified.
  155.        
  156.         Example:
  157.        
  158.             function OnPlayerKill(killer, victim, mode)
  159.            
  160.                 local khash = gethash(killer)
  161.                 local total_kills = table.sum(stats[khash].kills)
  162.                 privatesay(killer, "You have " .. total_kills .. " total kills.")
  163.             end
  164.            
  165.    
  166.     If you have any questions about how any of these functions work, PM me (Nuggets) at phasor.proboards.com.
  167. --]]
  168.  
  169. function table.save(t, filename)
  170.  
  171.     local dir = getprofilepath()
  172.     local file = io.open(dir .. "\\data\\" .. filename, "w")
  173.     local spaces = 0
  174.  
  175.     local function tab()
  176.  
  177.         local str = ""
  178.         for i = 1,spaces do
  179.             str = str .. " "
  180.         end
  181.  
  182.         return str
  183.     end
  184.  
  185.     local function format(t)
  186.  
  187.         spaces = spaces + 4
  188.         local str = "{ "
  189.  
  190.         for k,v in opairs(t) do
  191.             -- Key datatypes
  192.             if type(k) == "string" then
  193.                 k = string.format("%q", k)
  194.             elseif k == math.inf then
  195.                 k = "1 / 0"
  196.             end
  197.  
  198.             -- Value datatypes
  199.             if type(v) == "string" then
  200.                 v = string.format("%q", v)
  201.             elseif v == math.inf then
  202.                 v = "1 / 0"
  203.             end
  204.  
  205.             if type(v) == "table" then
  206.                 if table.len(v) > 0 then
  207.                     str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. format(v) .. ","
  208.                 else
  209.                     str = str .. "\n" .. tab() .. "[" .. k .. "] = {},"
  210.                 end
  211.             else
  212.                 str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. tostring(v) .. ","
  213.             end
  214.         end
  215.  
  216.         spaces = spaces - 4
  217.  
  218.         return string.sub(str, 1, string.len(str) - 1) .. "\n" .. tab() .. "}"
  219.     end
  220.  
  221.     file:write("return " .. format(t))
  222.     file:close()
  223. end
  224.  
  225. function table.load(filename)
  226.  
  227.     local dir = getprofilepath()
  228.     local file = loadfile(dir .. "\\data\\" .. filename)
  229.     if file then
  230.         return file() or {}
  231.     end
  232.    
  233.     return {}
  234. end
  235.  
  236. function table.len(t)
  237.  
  238.     local count = 0
  239.     for k,v in pairs(t) do
  240.         count = count + 1
  241.     end
  242.    
  243.     return count
  244. end
  245.  
  246. function table.find(t, v, case)
  247.  
  248.     if case == nil then case = true end
  249.  
  250.     for k,val in pairs(t) do
  251.         if case then
  252.             if v == val then
  253.                 return k
  254.             end
  255.         else
  256.             if string.lower(v) == string.lower(val) then
  257.                 return k
  258.             end
  259.         end
  260.     end
  261. end
  262.  
  263. function table.max(t)
  264.  
  265.     local max = -math.inf
  266.     local key
  267.    
  268.     for k,v in pairs(t) do
  269.         if tonumber(v) then
  270.             if tonumber(v) > max then
  271.                 key = k
  272.                 max = tonumber(v)
  273.             end
  274.         end
  275.     end
  276.    
  277.     return key,max
  278. end
  279.  
  280. function table.maxv(t)
  281.  
  282.     local max = -math.inf
  283.     local key
  284.    
  285.     for k,v in pairs(t) do
  286.         if tonumber(v) then
  287.             if tonumber(v) > max then
  288.                 key = k
  289.                 max = tonumber(v)
  290.             end
  291.         end
  292.     end
  293.    
  294.     return max 
  295. end
  296.  
  297. function table.maxes(t)
  298.  
  299.     local keys = {}
  300.     local max = -math.inf
  301.     for k,v in pairs(t) do
  302.         if tonumber(v) then
  303.             if tonumber(v) > max then
  304.                 max = tonumber(v)
  305.             end
  306.         end
  307.     end
  308.    
  309.     for k,v in pairs(t) do
  310.         if tonumber(v) == max then
  311.             table.insert(keys, k)
  312.         end
  313.     end
  314.    
  315.     return keys,max
  316. end
  317.  
  318. function table.sum(t, key)
  319.  
  320.     local sum = 0
  321.     for k,v in pairs(t) do
  322.         if type(v) == "table" then
  323.             sum = sum + table.sum(v, key)
  324.         elseif tonumber(v) then
  325.             if key then
  326.                 if key == k then
  327.                     sum = sum + tonumber(v)
  328.                 end
  329.             else
  330.                 sum = sum + tonumber(v)
  331.             end
  332.         end
  333.     end
  334.    
  335.     return sum
  336. end
Advertisement
Add Comment
Please, Sign In to add comment