Strange behaviour in Lua. Trying to build a sorted list


#1

The LargeList function did not quite suit my requirements. So I tried to build a custom sorted list by using a Lua script to manipulate a record with…

  • List of elements sorted in order
  • Map containing the scores of the elements

Am very new to the world of Lua scripting, so you need to bear with me.

I’m stuck because I cannot seem to do the simplest task of comparing A) the scores I stored in the map against B) the score of new key I passed into the Lua script.

for val in list.iterator(l) do
 i= i+ 1
 if (m[l[i]] > score) then
   <actionA>
 else
   <actionB>
 end

I know how silly it sounds, but whatever values I try, I cannot get the script to run actionA. For example,

  • m[l[i]] is 9 and score is 10 >>> actionB gets triggered
  • m[l[i]] is 11 and score is 10 >>> actionB gets triggered

#2

Stored inside the record…

users = l

users::score = m

{ bins: 
   { users: [ 'userA' ],
     'users::score': { userA: '10' }

#3

The score being stored in your example seems to a string. Did you really mean

{bins: 
    { 'users': ['userA']
       'users::score' : { 'userA': 10 }

– R


#6

Somehow, I was comparing 2 strings. Both m[l[i]] and score.

if (m[l[i]] > score) then

Needed to convert both to numbers using tonumber. Conversion is not automatic.


#7

Thanks! Glad it was resolved.