Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: Modify a txt file

by davido (Archbishop)
on Oct 20, 2011 at 06:56 UTC ( #932577=note: print w/replies, xml ) Need Help??

in reply to Re^2: Modify a txt file
in thread Modify a txt file

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).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932577]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-05-28 06:08 GMT
Find Nodes?
    Voting Booth?