Tables - Part #2

Nothing Fancy, just pure Lua tables for your learning. In part #1, we saw how to create/declare tables and the subtle differences between Arrays and Tables.

In this second part, let us look further into tables.

Firstly, this has been a very common issue with a lot of users, which is seen commonly breaking code in places where one does not expect it, it was addressed in the article here which discusses the fact that for arrays that require removing items, it is best if the array is transversed from bottom to the top than from top to bottom as many are tempted to using a for loop. A small little tip that can go a long way in ironing out the bugs.

Data Order - Sorting

I was working on an app where I needed to have data sorted and having programmed with Basic since the early 80's I thought it would be a pain to write my own sort routines, compare, etc... However I also knew that there was a native lua inbuilt command to sort the data, so the result was a pleasant surprise that provided me the ability to sort and it was so simple that I could not get over it. There is perhaps nothing that is more dreaded and simpler than sorting tables using lua.

So, here's the scenario, I was making a day graph for a client, the graph was supposed to show different type of activity throughout the day, since the data was to be retrieved and displayed by time rather than by type, it meant sorting the tables. The lua table.sort() command was used and this is how it is used
   table.sort(data, function(a,b) return a[1] < b[1] end)
All it requires is the table, which in this case is called data and a sorting function which is an in-line function provided in this instance, described as
    function(a,b) 
       return a[1] < b[1] 
    end
this function takes two parameters a and b, which are two elements of the table being compared, it then returns which one is to be sorted higher than the other, this can be used to have complex sorting if you so wish. That's all and the data in the table called data is *sorted*

Shuffling

Well, you are all here not because I have the gift of the keys, but to get the info on how to work with tables, so let's get to that bit too. How to shuffle? Shuffling, in simple terms, as when dealing with a pack of cards is mixing the order of the data randomly, such that when retrieved sequentially, it is different from the original order. There are quite a few different algorithms that detail how to shuffle, Wikipedia (the online modern encyclopedia for all information) states that
    To shuffle an array a of n elements (indexes 0..n-1):
       for i from n − 1 downto 1 do
           j <-- random integer with 0 ≤ j ≤ i
          exchange a[j] and a[i]
So, what we get in code is,
function shuffle(theTable)
  local theTable = theTable
  local n = #theTable
  local j
  local random = math.random

   for i=n-1, 1, -1 do
     j = random(i)
    theTable[j],theTable[i] = theTable[i],theTable[j]
   end

   return theTable
end
Another variation of the same algorithm states that one could use
  for i=n, 2, -1 do

That's all folks

Well, as promised, here are the two things that you were all waiting for, if there is anything else related to Tables that you want to know, let me know and let us see what we can do for you.

Comments

Post a Comment

Popular Posts