Back to Recipes Homepage
Profile
Best ForComments
Mon, 06/21/2010  03:22
#2
Easier way to do this
field = {1, 3, 2, 5, 4} This works for most tables. 2.5.5  The Length Operator The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte). The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array, with nonnil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other nonnil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array). 

function get_field_length(field)  checks how many entries are in a list
l=1
k=0
while (l~=nil) do
k=k+1
l=field[k]
end  while
return (k1)
end  function
field={1,3,2,5,4}
FL=get_field_length(field)
print("Field length is ",FL)