Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Remove RTF Formatting

by TomDLux (Vicar)
on Feb 28, 2011 at 23:35 UTC ( [id://890665]=note: print w/replies, xml ) Need Help??


in reply to Remove RTF Formatting

If it's going to remain under 20 lines, I have no problem with using the default $_.

But I've never known a problem to not develop exceptions, so you wind up with dozens or hundreds of lines of code. If it gets any more complicated, you should split code into subroutines, especially if some of the processing is conditional. In any case, is the verbose form slower than the default form?

Your first regex comment says remove lines starting with braces, but you don't enforce the starting part. I would personally process the file on a line-by-line basis, after all, you might get gigabyte submissions, and so would stop looking at a line as soon as I detected the curly brace. Note that \A is the beginning of the string, \z is the end of the string.

LINE: while (my $line = <$INFILE> ) { chomp next LINE if $line =~ m<\A\s*{>; # # or if it was going to be literally # the first char, with no spaces ... # next LINE if q<{> eq substr $line, 0, 1; # # other processing # $text .= $line . $NEWLINE; }

Lines with only a slash are easy:

next LINE if length $line == 1 and q{\} eq substr $line, 0, 1;

In #3, when removing leading spaces ( You don't care about spaces within the line, it seems), instead of copying the newline and the stuff after the space, why not use Look-Around Assertions to specify you only want to remove spaces that come after a newline. No need to say anything about what follows, it will grab all spaces and tabs.

s/(?<=\n)\s+//; #or, line by line: s/\A\s+//

Similarly in #4, don't copy the line stuff, just drop trailing spaces

s/\s+\n//;

I would predefined a constant, or at least a variable, for the double backslash and newline.

Readonly my $ESC_BACKSLASH => q{\\}; Readonly my $NEWLINE => q{\n}; $wholefile =~ s/$ESC_BACKSLASH\z//o;

In #2, don't capture the RTF command you want to discard, it just slows things down. AS for the word boundary \b, it isn't really doing anything useful, because greediness will make the hyphenated word slurp up everything it can.

Readonly my $RTF_CMD => qr/$ESC_BACKSLASH [\w-]+/xo; s/$RTF_CMD//;

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: Remove RTF Formatting
by jdporter (Paladin) on Mar 01, 2011 at 15:00 UTC
    if length $line == 1 and q{\} eq substr $line, 0, 1;

    How in the world is that better than

    if $line =~ /^\\$/;

    ?????

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

        Assuming it has been chomped!

Re^2: Remove RTF Formatting
by toolic (Bishop) on Mar 02, 2011 at 01:27 UTC
    Quoting is tricky.
    next LINE if length $line == 1 and q{\} eq substr $line, 0, 1;
    That's a compile error. I think you want q{\\}
    Readonly my $NEWLINE => q{\n};
    use Readonly; { # I don't think you want this Readonly my $NEWLINE => q{\n}; print ">>>$NEWLINE<<<\n"; } { # Do you really want this? Readonly my $NEWLINE => qq{\n}; print ">>>$NEWLINE<<<\n"; } __END__ >>>\n<<< >>> <<<
Re^2: Remove RTF Formatting
by GotToBTru (Prior) on Mar 01, 2011 at 14:59 UTC
    Good stuff here, thanks for taking the time. I developed these iteratively, and it shows. The parentheses on #2 had a purpose in an earlier version, and I didn't think to remove them when I stopped needing them.

    As I initially understood the project, I needed to strip all the formatting out of the file. In the actual application, I do end up processing line by line because all the data I need is in the first third of the file (these are only 200-300 lines long), and I actually process less than a dozen of the lines I do read. The real benefit of this project has been, as usual, my education.

Log In?
Username:
Password:

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

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

    No recent polls found