this should be a pretty simple problem, but it's got me at a dead end. i have a file with a set of semi-random marker strings, like so:
anfnf11
iopi1p83288
9032-jjjf
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:
i have <a href="http://www.somewhere.org/foo/">a link</a> and <a href=
+https://somewhere.else.net/bar/>the second link</a>.
after the script runs, it would look 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>
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:
# @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;
}
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
my $count = 0;
while ($htmlfile =~ m/<a\s+href\s*=\s*\"?(http[\s\>]+)"?>/gi){
$htmlfile =~ s/$1/$1$markers[$count]/;
$count ++;
}
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