0

I am working with the Redis Ruby Gem and and trying to accomplish some server side scripting in Lua, however I cannot convert one of my variables into the required string format.

My variable in it's natural form is: v = 495584087797551105

If I pass v = '495584087797551105' (note the single quotes) everything works fine and I get my desired output of 1.

print redis.eval(" local test 
                   local v 
                   v = '495584087797551105'
                   test = redis.call('sismember', 'testkey',v)
                   return test")
**Returns "1"**

However, I cannot convert 495584087797551105 to '495584087797551105'. I have tried using both tostring(v) and string.format('%d',v) and both return the incorrect 0 result.

print redis.eval(" local test 
                   local v 
                   local b

                   v = 495584087797551105
                   b = tostring(v)             --attempt 1
                   b = string.format('%d',v)   --attempt 2
                   test = redis.call('sismember', 'testkey',b)
                   return test")
**Returns "0"**

I have tried using double quotes inside my eval statement however in Ruby does not like it and throws an error. It seems like there has to be a simple solution.

tjrburgess
  • 757
  • 1
  • 8
  • 18
  • Your number is too big. See: http://en.wikipedia.org/wiki/Double-precision_floating-point_format -- 2^53 = 9007199254740992, less than your number. – Advert Aug 08 '14 at 20:50
  • Take a look at this, if you need to use math with big numbers: http://stackoverflow.com/questions/288707/what-is-the-standard-or-best-supported-big-number-arbitrary-precision-librar But if you don't, you're better off keeping your number as a string, since it won't be accurately represented as a double float. – Advert Aug 08 '14 at 21:04
  • @Advert does this prohibit me from using ipairs? My full Lua code grabs an array from Redis (containing strings) and then iterates through using ipairs for an input into a subsequent redis call. ipairs seems to convert the values (v in the above) into a format I can't pass as an argument to Redis. – tjrburgess Aug 08 '14 at 21:27
  • Yes. ipairs will only run over consecutive integers: http://ideone.com/OzYaaU – Advert Aug 08 '14 at 21:34
  • That is strange, the value shouldn't be affected at all by `ipairs/pairs/next`. Perhaps redis/something else is trying to convert them to numbers? – Advert Aug 08 '14 at 21:40
  • i've replaced the ipairs with a for loop and everything seems to be working fine. Fingers crossed it stays that way! – tjrburgess Aug 09 '14 at 12:14

0 Answers0