Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Modify a txt file

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


in reply to Modify a txt file

If you look at each grouping as a record, and set the input record separator to "\n\n" (which is what appears to separate the records in your example), it gets really easy:

use strict; use warnings; $/ = "\n\n"; while ( <DATA> ) { if( my( $number ) = m/(\d+)/ ) { s/\b\p{Alpha}+\b/$number/g; } else { warn "Malformed record in input line $.\n$_\nContinuing.\n"; } print; } __DATA__ 0 ASDF ASEE ASEE 13 DERG DREG 28 QWER QWER 42 WERT WERT WERT 55 QWEASD QWEASD QWEASD QWEASD

Here is the output from your test data:

0 0 0 0 13 13 13 28 28 28 42 42 42 42 55 55 55 55 55

Dave


Comment on Re: Modify a txt file
Select or Download Code
Re^2: Modify a txt file
by la (Novice) on Oct 20, 2011 at 05:51 UTC

    Wow! Thanks everyone for all of the help! I will work on it and let you know how it goes. Thanks again :)

Re^2: Modify a txt file
by la (Novice) on Oct 20, 2011 at 06:37 UTC

    Hey Dave, Thanks for the advice. Although I am using your code, I am not getting the correct output...

    Code I am using:

    #!/usr/bin/perl use warnings; use strict; if($#ARGV<0){ die "Usage: $0 <*.txt>\n"; } open(IN,$ARGV[0]) ; $/ = "\n\n"; while ( <IN> ) { if( my( $number ) = m/(\d+)/ ) { s/\b\p{Alpha}+\b/$number/g; } else { warn "Malformed record in input line $.\n$_\nContinuing.\n"; } print; }

    Input:

    0 ASDF ASEE ASEE 13 DERG DREG 28 QWER QWER 42 WERT WERT WERT 55 QWEASD QWEASD QWEASD QWEASD

    Getting this output:

    0 0 0 0 13 0 0 28 0 0 42 0 0 0 55 0 0 0 0

    Desired Output:

    0 0 0 0 13 13 13 28 28 28 42 42 42 42 55 55 55 55 55

    I see that it is in this line s/\b\p{Alpha}+\b/$number/g; where the substitution is being made. Is the code referring back to the original 0 in the top left hand column perhaps?

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


      Dave

      The code you posted works as advertised by davido. Consider:

      use strict; use warnings; my $data = <<DATA; 0 ASDF ASEE ASEE 13 DERG DREG 28 QWER QWER 42 WERT WERT WERT 55 QWEASD QWEASD QWEASD QWEASD DATA open my $inFile, '<', \$data; $/ = "\n\n"; while (<$inFile>) { if (my ($number) = m/(\d+)/) { s/\b\p{Alpha}+\b/$number/g; } else { warn "Malformed record in input line $.\n$_\nContinuing.\n"; } print; }

      Prints:

      0 0 0 0 13 13 13 28 28 28 42 42 42 42 55 55 55 55 55

      so there is a mismatch between what you are telling us and what you are actually doing. If you really want help you need to really tell us what you are doing and show us (as per the sample code above) how things are going wrong. We can't fix what ain't broke!

      True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932563]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-12-25 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls