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

In reply to appending a unique marker to each url in a file by cat2014

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.