pseudosocrates has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am writing a script that jumbles the text of links, where the target is a numeric page, on a webpage; I have a match that pulls all the text off the links into an array, and I now want to put those array contents randomly back in to the page (not worried if they are duplicated).
$_ = $content; #content holds the html while (m@href=\"\./\d+\.html\">(.+)</a>@g) {push (@array, $1);}
This is where I'm stuck - I want to use @array to replace the text back between the tags randomly. Thanks for any help, hope I was clear.

Replies are listed 'Best First'.
Re: Regex partial substitution from array
by sauoq (Abbot) on Dec 15, 2004 at 18:28 UTC

    Something like the following untested line might help...

    s!(href=\"\./\d+\.html">)(.+)(</a>)!$1.splice(@array, rand(@array), 1) +.$3!ge

    -sauoq
    "My two cents aren't worth a dime.";
    
      Heh, with the hastily added right-bracket, that works just dandy, thanks a lot :-)
Re: Regex partial substitution from array
by Stevie-O (Friar) on Dec 15, 2004 at 19:54 UTC
    I'd just like to point that
    while (m@href=\"\./\d+\.html\">(.+)</a>@g) {push (@array, $1);}
    can be better written as
    @array = m@href=\"\./\d+\.html\">(.+)</a>@g;
    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc