|Don't ask to ask, just ask|
Re^3: Modify a txt fileby davido (Archbishop)
|on Oct 20, 2011 at 06:56 UTC||Need Help??|
Why do you suppose this is happening? Have you taken any steps besides posting to figure out why the solution isn't working for you?
When debugging it's often helpful to check the state of the program's logic at one or more points. An easy way to do this is with "print" statements that give you clues as to where you are within the program's control flow.
For example, if you added a print "Record: $.\n"; statement as the first line of the block of your while() loop you would see each time the loop iterates over a new record. And if you added print "New match: $number\n"; as the first line of the if() block, you would see each time a new number is matched and captured into $number. After running the script with those two debugging aids you would probably see that the file is being read in as one big record, rather than as multiple records.
That seems impossible if your input data matches the data you showed us, and if you're executing the code you say you are. Either you've got $/ = ''; in your code, or you have data that isn't separated by two newlines like it appears in your post. At least those are my best guesses without seeing exact cut&pastes of the first few records of your data, and of the script exactly as it's being run.
For what it's worth, I copied and pasted the exact data you posted here and used that as the sample run for my solution. I also copied and pasted the exact data that you posted in your followup, and it also produced the correct results.
Is it possible that you're re-typing the data rather than copy/pasting it, and that the blank line between records actually contains some space characters that we can't see, and that you didn't paste into your example data?
By the way: This isn't contributing to your problem, but it is a darn good idea anyway: Put use autodie; right after the use warnings; line at the top of your script. That will alert you if a file fails to open (among other things).