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

Re: Re: Title Re: collapsing revisited

by Dog and Pony (Priest)
on Jun 08, 2002 at 15:55 UTC ( [id://172799]=note: print w/replies, xml ) Need Help??


in reply to Re: Title Re: collapsing revisited
in thread Title Re: collapsing revisited

Yep, jeffa pointed out a similar example ("2Re: "). That is another good reason to have it as a user setting. It isn't meant to enforce a style upon someone, after all...

The solution would probably be to be able to set which style you prefer for out put, then have the engine look for all those cases and transforming them.

I might take a stab at it later, but for adding that extra complexity, it would have to be both viable to do, and enough people that wants it.

I had seen a few posts with Re^2 and friends, but I didn't think they were that common (plus this is more of a draft and first example). If we can have all the ways, I'm all for it. :)

Personally though, I find the Re^2: style harder to read, even though perhaps more logically correct. :)

Update:

Ok, I took the challenge. Following here is a new version, that handles both types (though not the "2Re:" style, at least not yet). It is about time to start refactoring though, it would seem, as it is getting really hairy. As a demonstration though, it will serve I think.

Now we have some new subs, instead of the old ones, we call the same ones with an added _caret for the "Re^2:" style, and _paranthesis for the "Re(2):" style. This goes for both re_collapse_* and re_add_*. As an added bonus, it will also translate from one type to the chosen one, so you can have it all your style.

Given this approach, and some refactoring, it should be possible to add yet more types. I'm not 100% sure this will hold for every possible case, but the tests I do have seems to indicate it would hold for most stuff.

It also has removed the $& as was pointed out though I din't understand why at first... doh. :) But then, is @- and @+ free from this? I hope so.

Anyhow, here it is - enjoy:

#!/usr/bin/perl -w use strict; use Test; BEGIN { plan tests => 49; } # Collapses sequences of Re: together into one Re(\d): sub re_collapse_parenthesis { return &re_collapse($_[0], "Re(", "): "); } # Collapses sequences of Re: together into one Re^\d: sub re_collapse_caret { return &re_collapse($_[0], "Re^", ": "); } # Collapses sequences together base on $pre and $post: sub re_collapse { my $title = shift; my ($pre, $post) = @_; # Normal 'Re: Re: ' sequences $title =~ s{(Re: ){2,}}{$pre . ($+[0]-$-[0])/4 . $post}ge; # 'Re(\d):' => 'Re^\d:' and vice versa $title =~ s{Re(?:\((\d+)\)|\^(\d+)): }{$pre . ($1||$2) . $post}ge; # Already renumbered: # 'Re: Re(\d+): ' or the Re^\d counterpart while($title =~ s{Re: Re(?:\((\d+)\)|\^(\d+)): } {$pre . (($1||$2) + 1) . $post }ge){}; # 'Re(\d+): Re: ' or the Re^\d counterpart while($title =~ s{Re(?:\((\d+)\)|\^(\d+)): Re: } {$pre . (($1||$2) + 1) . $post }ge){}; # 'Re(\d+): Re(\d+): ' or the Re^\d counterpart while($title =~ s{Re(?:\((\d+)\)|\^(\d+)): Re(?:\((\d+)\)|\^(\d+)): } {$pre . (($1||$2) + ($3||$4)) . $post}ge){}; return $title; } # Adds a new Re: or Re(\d): at the start of a string: sub re_add_parenthesis { return &re_add($_[0], "Re(", "): "); } # Adds a new Re: or Re^\d: at the start of a string: sub re_add_caret { return &re_add($_[0], "Re^", ": "); } # Adds a new Re: etc. based on $pre and $post: sub re_add { my $title = shift; my ($pre, $post) = @_; $title =~ s{^(Re(\((\d+)\)|\^(\d+))?: )?} {($1) ? ($pre . (($3||$4||1) + 1) . $post) : "Re: "}e; return $title; } ########################### # # Tests: # # Testing re_collapse_parenthesis: # Should not change: ok(&re_collapse_parenthesis("(DaP) Re: Foo is not Bar") eq "(DaP) Re: +Foo is not Bar"); ok(&re_collapse_parenthesis("(DaP) Foo is not Bar") eq "(DaP) Foo is n +ot Bar"); ok(&re_collapse_parenthesis("Re(3): (DaP) Re: Foo is not Bar") eq "Re( +3): (DaP) Re: Foo is not Bar"); # Normal Re: sequences: ok(&re_collapse_parenthesis("Re: Re: Foo is not Bar") eq "Re(2): Foo i +s not Bar"); ok(&re_collapse_parenthesis("Re: Re: Re: Foo is not Bar") eq "Re(3): F +oo is not Bar"); ok(&re_collapse_parenthesis("Re: Re: (DaP) Re: Foo is not Bar") eq "Re +(2): (DaP) Re: Foo is not Bar"); ok(&re_collapse_parenthesis("Re: Re: (DaP) Re: Re: Foo is not Bar") eq + "Re(2): (DaP) Re(2): Foo is not Bar"); # Already renumbered: ok(&re_collapse_parenthesis("Re: Re(2): Foo is not Bar") eq "Re(3): Fo +o is not Bar"); ok(&re_collapse_parenthesis("Re(2): Re: Foo is not Bar") eq "Re(3): Fo +o is not Bar"); ok(&re_collapse_parenthesis("Re: Re(2): (DaP) Re: Re(2): Foo is not Ba +r") eq "Re(3): (DaP) Re(3): Foo is not Bar"); ok(&re_collapse_parenthesis("Re(2): Re: Re: Re(2): Foo is not Bar") eq + "Re(6): Foo is not Bar"); ok(&re_collapse_parenthesis("Re: Re(2): (DaP) Re(2): Re: Foo is not Ba +r") eq "Re(3): (DaP) Re(3): Foo is not Bar"); ok(&re_collapse_parenthesis("Re: Re: Re(2): (DaP) Re(2): Re: Re(4): Re +: Foo is not Bar") eq "Re(4): (DaP) Re(8): Foo is not Bar"); # Example from node id 168373: # (Nothing much to do about such). ok(&re_collapse_parenthesis("Re: Re: (Someone) Re: (Someoneelse) Re: ( +Someotherelse) Re: Re: Something") eq "Re(2): (Someone) Re: (Someonee +lse) Re: (Someotherelse) Re(2): Something"); # Testing Re^2 style: ok(&re_collapse_parenthesis("Re: Re^2: Foo is not Bar") eq "Re(3): Foo + is not Bar"); ok(&re_collapse_parenthesis("Re^2: Re: Foo is not Bar") eq "Re(3): Foo + is not Bar"); ok(&re_collapse_parenthesis("Re: Re^2: (DaP) Re: Re^2: Foo is not Bar" +) eq "Re(3): (DaP) Re(3): Foo is not Bar"); ok(&re_collapse_parenthesis("Re^2: Re: Re: Re^2: Foo is not Bar") eq " +Re(6): Foo is not Bar"); ok(&re_collapse_parenthesis("Re: Re^2: (DaP) Re^2: Re: Foo is not Bar" +) eq "Re(3): (DaP) Re(3): Foo is not Bar"); ok(&re_collapse_parenthesis("Re: Re: Re^2: (DaP) Re^2: Re: Re^4: Re: F +oo is not Bar") eq "Re(4): (DaP) Re(8): Foo is not Bar"); ####################### # Testing re_collapse_caret: # Should not change: ok(&re_collapse_caret("(DaP) Re: Foo is not Bar") eq "(DaP) Re: Foo is + not Bar"); ok(&re_collapse_caret("(DaP) Foo is not Bar") eq "(DaP) Foo is not Bar +"); # Normal Re: sequences: ok(&re_collapse_caret("Re(3): (DaP) Re: Foo is not Bar") eq "Re^3: (Da +P) Re: Foo is not Bar"); ok(&re_collapse_caret("Re: Re: Foo is not Bar") eq "Re^2: Foo is not B +ar"); ok(&re_collapse_caret("Re: Re: Re: Foo is not Bar") eq "Re^3: Foo is n +ot Bar"); ok(&re_collapse_caret("Re: Re: (DaP) Re: Foo is not Bar") eq "Re^2: (D +aP) Re: Foo is not Bar"); ok(&re_collapse_caret("Re: Re: (DaP) Re: Re: Foo is not Bar") eq "Re^2 +: (DaP) Re^2: Foo is not Bar"); # Already renumbered: ok(&re_collapse_caret("Re: Re(2): Foo is not Bar") eq "Re^3: Foo is no +t Bar"); ok(&re_collapse_caret("Re(2): Re: Foo is not Bar") eq "Re^3: Foo is no +t Bar"); ok(&re_collapse_caret("Re: Re(2): (DaP) Re: Re(2): Foo is not Bar") eq + "Re^3: (DaP) Re^3: Foo is not Bar"); ok(&re_collapse_caret("Re(2): Re: Re: Re(2): Foo is not Bar") eq "Re^6 +: Foo is not Bar"); ok(&re_collapse_caret("Re: Re(2): (DaP) Re(2): Re: Foo is not Bar") eq + "Re^3: (DaP) Re^3: Foo is not Bar"); ok(&re_collapse_caret("Re: Re: Re(2): (DaP) Re(2): Re: Re(4): Re: Foo +is not Bar") eq "Re^4: (DaP) Re^8: Foo is not Bar"); # Example from node id 168373: # (Nothing much to do about such). ok(&re_collapse_caret("Re: Re: (Someone) Re: (Someoneelse) Re: (Someot +herelse) Re: Re: Something") eq "Re^2: (Someone) Re: (Someoneelse) Re +: (Someotherelse) Re^2: Something"); # Testing Re^2 style: ok(&re_collapse_caret("Re: Re^2: Foo is not Bar") eq "Re^3: Foo is not + Bar"); ok(&re_collapse_caret("Re^2: Re: Foo is not Bar") eq "Re^3: Foo is not + Bar"); ok(&re_collapse_caret("Re: Re^2: (DaP) Re: Re^2: Foo is not Bar") eq " +Re^3: (DaP) Re^3: Foo is not Bar"); ok(&re_collapse_caret("Re^2: Re: Re: Re^2: Foo is not Bar") eq "Re^6: +Foo is not Bar"); ok(&re_collapse_caret("Re: Re^2: (DaP) Re^2: Re: Foo is not Bar") eq " +Re^3: (DaP) Re^3: Foo is not Bar"); ok(&re_collapse_caret("Re: Re: Re^2: (DaP) Re^2: Re: Re^4: Re: Foo is +not Bar") eq "Re^4: (DaP) Re^8: Foo is not Bar"); ####################### # Testing re_add_parenthesis: ok(&re_add_parenthesis("Foo is not Bar") eq "Re: Foo is not Bar"); ok(&re_add_parenthesis("Re: Foo is not Bar") eq "Re(2): Foo is not Bar +"); ok(&re_add_parenthesis("Re(2): Foo is not Bar") eq "Re(3): Foo is not +Bar"); # This one has to be collapsed later, so this is ok: ok(&re_add_parenthesis("Re: Re: Foo is not Bar") eq "Re(2): Re: Foo is + not Bar"); ####################### # Testing re_add_caret: ok(&re_add_caret("Foo is not Bar") eq "Re: Foo is not Bar"); ok(&re_add_caret("Re: Foo is not Bar") eq "Re^2: Foo is not Bar"); ok(&re_add_caret("Re(2): Foo is not Bar") eq "Re^3: Foo is not Bar"); ok(&re_add_caret("Re^2: Foo is not Bar") eq "Re^3: Foo is not Bar"); # This one has to be collapsed later, so this is ok: ok(&re_add_caret("Re: Re: Foo is not Bar") eq "Re^2: Re: Foo is not Ba +r"); #######################


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found