title: Lua script for Redis which sums the values of keys
body: <p>I am building out my first Redis server side script (<em>for debugging</em>) and my lack of Lua experience has me quite stuck.</p>
  
  <p>Essentially have a dataset of K/V pairs (containing ~1000 values) from which I want to list all the KEYS that match a pattern. For example in redis-cli:</p>
  
  <pre><code>&gt; KEYS "carlos:*"
  1) "carlos:1"
  2) "carlos:2"
  3) "carlos:3"
  4) "carlos:4"
  </code></pre>
  
  <p>Based on the above output I want to return the sum of those keys by executing a Lua script. Currently I have the following on my <code>sum.lua</code></p>
  
  <pre><code>local sum = 0
  local matches = redis.call('KEYS', 'carlos:*')
  
  for unpack(matches)
     sum = sum + redis.call('GET', matches)
  end
  
  return sum
  </code></pre>
  
  <p>While the above script is likely incorrect, trying even <code>redis.call('KEYS', 'carlos:*')</code> by itself produces the following error</p>
  
  <blockquote>
    <p>root@carlos:~# redis-cli EVAL "$(cat sum.lua)"</p>
    
    <p>(error) ERR wrong number of arguments for 'eval' command</p>
  </blockquote>
  
  <p>I have tried a number of iterations of my syntax to no avail. Any ideas?</p>
  
  <p>Thanks</p>