You don't need $calc() with /vars ... at least with a basic calculation as was used.
The error is in:
while (%t) var %s = %s + $len($hget(%table,%i).item), %s = %s + $len($hget(%table,%i).data), %t = %t - 1
%t needs to be used instead of %i in both $hget() sections. That's the only change necessary, but RusselB's is a bit easier to read.