in reply to help required for regexp

The first part of your regular expression would only match:
f3333
And therefore replace 3333 with 1. Also, you haven't mentioned the %labels hash but used it in regexp.

Your regexp can be made more specific and changed to:
 $formula=~s/([0-9]{4})/$Hidlabels{$1}/g;