cat2014 has asked for the wisdom of the Perl Monks concerning the following question:
and a html page which has a bunch of links inside it. my problem seemed really simple when i started this- i want to take the first marker string, and stick it on the end of the first link, then take the second marker string, and append it to the end of the second link. I can guarantee that all the links start with http:// or https://, so i'm looking for that in my regex. this is what my text would look like before the script runs:anfnf11 iopi1p83288 9032-jjjf
after the script runs, it would look like this:i have <a href="http://www.somewhere.org/foo/">a link</a> and <a href= +https://somewhere.else.net/bar/>the second link</a>.
theoretically, the number of links in the html page is the same as the number of marker strings, but i'd like to fail gracefully if i run out of markers or if there are more markers than links. anyway, i thought it would be no problem- slurp the markers into an array, then do a substitution on the values of the links with the marker at the end. i started with something like this:<body>this is <a href="http://www.somewhere.org/foo/anfnf11">a link</a +> and <a href=https://somewhere.else.net/bar/iopi1p83288>the second l +ink</a>
That, of course, didn't work- the first link ended up with all the markers at the end of it. I tried a while loop, too:# @markers already holds each marker in each array spot #$htmlfile already has the text of my html file foreach my $m (@markers){ $htmlfile =~ s/\G<a\s+href\s+=\s+\"?(http[\s\>]+)"?>/<a href="$1$m +">/gi; }
The while loop feels like it's close, but it's not working,either- no markers end up in the output. So I'm kind of stuck here-- I feel like the solution to this is really simple, and I'm just missing it entirely. -- cat# @markers already holds each marker in each array spot #$htmlfile already has the text of my html file my $count = 0; while ($htmlfile =~ m/<a\s+href\s*=\s*\"?(http[\s\>]+)"?>/gi){ $htmlfile =~ s/$1/$1$markers[$count]/; $count ++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: appending a unique marker to each url in a file
by thatguy (Parson) on Aug 08, 2001 at 07:54 UTC | |
by Hofmator (Curate) on Aug 08, 2001 at 14:14 UTC | |
by tachyon (Chancellor) on Aug 08, 2001 at 17:36 UTC | |
by chipmunk (Parson) on Aug 08, 2001 at 17:59 UTC | |
by tachyon (Chancellor) on Aug 08, 2001 at 18:32 UTC | |
|
Re: appending a unique marker to each url in a file
by Cubes (Pilgrim) on Aug 08, 2001 at 08:12 UTC | |
|
Re: appending a unique marker to each url in a file
by chipmunk (Parson) on Aug 08, 2001 at 17:50 UTC | |
|
My end solution to appending a unique marker to each url in a file
by cat2014 (Monk) on Aug 09, 2001 at 02:27 UTC |