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

foreach my $line (@results) { my ($first, $second, $third) = split(/::/, $line); print qq(<b>$first</b> <a href="$second&a=$first&t=$third">$third +</a><br>); }
It prints out something like..
<b>page 1</b> <a href="urlhere?a=page 1&t=descrption 1">description 1< +/a><br> <b>page 2</b> <a href="urlhere?a=page 1&t=descrption 2">description 2< +/a><br> <b>page 3</b> <a href="urlhere?a=page 1&t=descrption 3">description 3< +/a><br>
In the printout, SOMETIMES it fills in the t= param but sometimes not. This is where the problem lies because it doesn't make any sense. If the var $third prints out the text for the link properly (between the A and /A), why doesn't it put that value in the parameter like I need?

For all my hundred or so printouts on the screen, the text for the link ALWAYS works and about 95% of the time the t= url param I'm passing that text to works too, I just need to figure out why it's not always passing the information.

Replies are listed 'Best First'.
Re: weird print results
by McDarren (Abbot) on Jul 01, 2006 at 17:54 UTC
    Odd.
    My guess would be that some of the data in @results is not formed in the way that you expect.
    A couple of suggestions:
    • Use Data::Dumper::Simple to dump the contents of @results before you enter the foreach loop. This should show you if you have any malformed data.
    • Does the data include newlines? Maybe you simply need to chomp
    • Post a sample of the data that is causing it to fail as you describe.

    Cheers,
    Darren :)

Re: weird print results
by Ieronim (Friar) on Jul 01, 2006 at 17:59 UTC
    It just can't be :) check your data for consistency:
    foreach my $line (@results) { my ($first, $second, $third) = split(/::/, $line); print "ERROR: \$third is empty!\n" unless $third; print qq(<b>$first</b> <a href="$second&a=$first&t=$third">$third +</a><br>); }
      The problem with trying that is it can't possibly be empty. It prints out $third as the link's text just fine (and it's the proper text).
        just try :)
Re: weird print results
by imp (Priest) on Jul 01, 2006 at 19:03 UTC
    The problem as stated isn't really solveable - we lack the data or actual output (you said 'something like').

    When you say that t isn't populated are you commenting on the html source, or where the link goes when you actually click on it?

    If the latter, then perhaps one of the values being output needs to be escaped.

Re: weird print results
by shmem (Chancellor) on Jul 01, 2006 at 18:04 UTC
    foreach my $line (@results)
    Sample data for @results would be handy.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        There are 10 snippets there. Take the guesswork out and show us!
Re: weird print results
by Mr. Muskrat (Canon) on Jul 01, 2006 at 17:57 UTC
    This looks like an HTML quoting issue. Properly quote your URL parameters and your problem should be solved.
    foreach my $line (@results) { my ($first, $second, $third) = split(/::/, $line); print qq(<b>$first</b> <a href="$second&a='$first'&t='$third'">$thi +rd</a><br>); }

    Update: I just removed the strikethrough and comment "Brainfart. Ignore me." that I added right after posting it. I added them because I was ticked off at the response I got in the CB.

      I could stil be a quoting issue. For exmple, spaces are not allowed in URLs. Similarly, the characters <, > and & have special meaning in HTML text, so they need to be escaped too.
      use CGI qw( escapeHTML ); use URI::Escape qw( uri_escape ); foreach my $line (@results) { my ($first, $second, $third) = split(/::/, $line); my $ue_first = uri_escape($first); my $ue_second = uri_escape($second); my $ue_third = uri_escape($third); my $he_first = escapeHTML($first); my $he_third = escapeHTML($third); print qq(<b>$he_first</b> <a href="$ue_second?a=$ue_first&t=$ue_thi +rd">$he_third</a><br>); }

      Alternate syntax:

      use CGI qw( escapeHTML ); use URI::Escape qw( uri_escape ); foreach my $line (@results) { my ($first, $second, $third) = split(/::/, $line); my ($ue_first, $ue_second, $ue_third) = map uri_escape($_), $first, $second, $third; my ($he_first, $he_second, $he_third) = map escapeHTML($_), $first, $second, $third; print qq(<b>$he_first</b> <a href="$ue_second?a=$ue_first&t=$ue_thi +rd">$he_third</a><br>); }

      No need to escape for HTML that which has already been escaped for URIs since URIs can't contain any special HTML characters.

        Thank you a hundred qadbillion! Escaping everything did make everything work 100% of the time.

        Thank you for your help, I really appreciate it!