Need help - code snippet to filter hostname

I have a script that runs periodically to convert the hostnames given in DHCP leases into static DNS entries.

Unfortunately, some of the DHCP clients use illegal characters, which result in less-than-useful DNS entries.

I’ve tried to engineer a snippet to fix this, but I can’t quite wrap my brain around it.

I want to loop through a string, removing any characters not in [-0-9a-zA-Z].

This loops through the name, extracting only [-0-9a-zA-Z] characters, saving them as the newName variable:
:local name User_Name!!-23
:local newName

:for i from=0 to=([:len $name] - 1) do={
:if ([:pick $name $i]~“[-0-9a-zA-Z]”) do={
:set newName ($newName . “$[:pick $name $i]”)
}
}
:put $newName

That worked! Thanks!