in reply to Changing input file

I suggest that when reading the records in this file you local change the record-separator $/ to }, so you read each record in one step (avoiding messy issues with newlines etc.)

Then it is just a matter of s/MONKS.+?(2329)/9999/

I'm not sure what you mean with $flag etc. but it has probably some meaning in your program.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law