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

Re: Randomly regex substitute

by samarzone (Pilgrim)
on Dec 24, 2010 at 07:15 UTC ( #878979=note: print w/ replies, xml ) Need Help??


in reply to Randomly regex substitute

Yet another solution (without claiming efficiency over previous solutions)

#!/usr/bin/perl -wl use strict; my $str = "boy boy girl boy girl boy girl girl"; my $l = int(rand(rindex($str,"boy") - 1)); $str =~ s/^(.{$l}.*?)\bboy\b/$1man/; print $str;
--
Regards
- Samar


Comment on Re: Randomly regex substitute
Download Code
Re^2: Randomly regex substitute
by oko1 (Deacon) on Dec 24, 2010 at 18:06 UTC

    Unfortunately, this one isn't very random - at least with regard to the first occurrence of 'boy'. I see where you tried to correct it with the '- 1', but that doesn't work:

    #!/usr/bin/perl -wl use strict; my %data; for (1..10000){ my $str = "boy boy girl boy girl boy girl girl"; my $l = int(rand(rindex($str,"boy") - 1)); $str =~ s/^(.{$l}.*?)\bboy\b/$1man/; $data{$str}++; } print "$_: $data{$_}" for sort keys %data;

    Output:

    boy boy girl boy girl man girl girl: 3323 boy boy girl man girl boy girl girl: 4310 boy man girl boy girl boy girl girl: 1881 man boy girl boy girl boy girl girl: 486

    (Don't ask me why I know about this problem. :)))


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2015-07-03 02:55 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 (47 votes), past polls