I have seen it often enough that you shouldnt use identifiers in while loops, so i made this change
var %chans = $chan(0)
while (%x < %chans) {
only reason not to use identifiers whose values dont change in the while loop is to make it faster, by ensuring the same identifier isnt needlessly re-evaluated every iteration.. though we all know the little bit of speed difference in this case is hardly noticable

however, it is shorter, and even 'faster' to loop like so:
while ($chan(%x)) {
then use $v1 in place of $chan(%x) in the rest of the loop