It is a very nice solution, thank you.
Is it possible, somehow to not to start with a reasonably big number? $result[0] = 10 ** 6; I don't know the 'max' id it get.
By the way, in @result = @fields[1,0,-1]; how does it work? what is the '-1' in the end?