Because you're refencing the same ID that you're wanting to set

Change the green line to
Code:
 else { did -c $dname 2 $did($dname,1).sel } 
and it should work.