Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Substitution don't work with a special inputfile

by poj (Abbot)
on Aug 24, 2015 at 16:28 UTC ( [id://1139705]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Substitution don't work with a special inputfile
in thread Substitution don't work with a special inputfile

Not sure if this is what you want as it also removes the newlines and re-inserts them before the 1. 2. etc

#!perl # Aufruf: perl removeEK1.pl TestEK.txt > Out.txt use strict; use warnings; my $regex = '{\[%tqu[^\]]*]}'; my $line = do { local $/; <>; }; $line =~ s/\n/ /g; $line =~ s/$regex//gi; $line =~ s/ (\d+\. )/\n$1/g; print $line;
poj

Replies are listed 'Best First'.
Re^4: Substitution don't work with a special inputfile
by OldChamp (Acolyte) on Aug 24, 2015 at 17:44 UTC

    Hi poj, thank you so much for your help, this is the first solution that does the job, but only, when the newlines were also re-inserted in the so called Header or tags, that are the strings starting with a square bracket. As you might have supposed, its a special text file with chess moves, but each of the square brackets has to be on its own line, otherwise a chess program can not interpret the file. As I'm new to perl, I have to work hard to understand your code, but I will try my best. The line: $line =~ s/ (\d+\. )/\n$1/g; does to my understanding the job of re-inserting the newlines but only with the numbered lines. I have added the line: $line =~ s/ (\[ )/\n$1/g; and I had hoped this will re-install the newlines for the tags, but for no good. If you can post a solution with newlines also for the tags, I would be very thankful to you.

      $line =~ s/\n/ /g;

      I don't understand this statement in poj's code: why are all newlines being changed to spaces?

      $line =~ s/ (\d+\. )/\n$1/g;

      This statement seems to speak to a requirement to add newlines before every  1.  23.  345. pattern, but I can't see where this has ever been discussed before. Please give more info on this.

      It would help if you could post a short example of an input file and a corresponding file representing the exact output you want from it. By short, I mean that most of the data in the input file you have already posted seems redundant; please try to keep example data short and sweet.

      And once again, the statement that the result of a piece of code is "no good" is not helpful; in what way is it no good? You should say "With this code and this input data, I want to get 'XXX' and I am getting 'YYY'."


      Give a man a fish:  <%-{-{-{-<

      Your addition should have worked but anyway try this

      #!perl # Aufruf: perl removeEK1.pl TestEK.txt > Out.txt use strict; use warnings; my $regex = '{\[%tqu[^\]]*]}'; my $line = do { local $/; <>; }; $line =~ s/\n/ /g; $line =~ s/$regex//gi; $line =~ s/ (\d+\. |\[)/\n$1/g; print $line;
      poj

        Thank you poj for helping me again, now it works like a charm. I have tried to solve the remaining problem by myself like this:

        #!/usr/bin/perl # Remove extra text from a PGN or Text-file. use strict; use warnings; my $regex = '{\[%tqu[^\]]*]}'; my $line = do { local $/; <>; }; $line =~ s/\n/ /g; $line =~ s/$regex//gi; # these statements I have inserted $line =~ s/\]/\]\n/g; $line =~ s/\[E/\n\[E/g; # end $line =~ s/ (\d+\. )/\n$1/g; print $line;

        and it "nearly" worked, but I got an empty first line in the outputfile.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found