how to sub using undefined hash item

Posted by restackhouse on 2016-12-02 06:52

I have a hash $X{'*1'}="A"; $X{'*2'}="B";

and

while(<>) { s/(\*[0-9]+)/$X{$1}/g }

input lines are like

*1
*1 *1 *2 *1

Problem is a line like

this *3 is an errant line

returns

this is an errant line

I can search for 2 spaces, but I'd have to remove spaces first.

Is there a way to say "don't do the substitutuion if $X{$1} doesn't exist without breaking into more wordy PERL?