Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Golf (Inspired): Repeated Phrases

by Corion (Pope)
on May 10, 2001 at 20:05 UTC ( #79450=note: print w/ replies, xml ) Need Help??


in reply to Golf (Inspired): Repeated Phrases

My solution is not really a competitor, since I'm not that familiar with Perl Golf customs, so bear with me.

use strict; sub f { my ($a,$b,$m,$n) = @_; my (@r) = (); my $l = " $a! $b"; while($l =~ s/(.*?)(( \w+){$m,$n} )(.*!.*\2.*)/ $1 =$4/) { push @r, $2; }; @r };

Why I'm posting this at all is, that a previous version exhibited unanticipated (by me, that is) behaviour :

# Previous version : my $l = " $a! $b"; while($l =~ s/(.*)(( \w+){$m,$n} )(.*!.*\2.*)/ $1 =$4/) { push @r, $2; }; @r

That version only found the $m-word matches, because the greedy (.*) at the beginning forced the RE engine to work its way backwards through the string. As soon as I sacrificed another character, this problem went away. I haven't given the golfing much thought (at work, I can only think about concepts, not byte-fiddle with REs without raising suspicions ... ).


Comment on Re: Golf (Inspired): Repeated Phrases
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2015-07-04 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls