while ($chan(%x)) {

this is an infinite loop as $chan(0) (the decreased value of $chan(x) is zero) you end up with all your channels in the var and then iinfinite $chan(0) appended to the end until the "* /set: line too long " error stops the loop.