Without looking into your problem, the @$x !~ /$digit{$xx}/ looks wrong. Since you compare the number of items in the array $x reference to with the string digit{$xx}. I think you want "@$x" !~ /$digit{$xx}/.
Update:revdiablo noticed that I wrote != but I mean !~. Fixed now.