t.b.b. has asked for the wisdom of the Perl Monks concerning the following question:

Gentle Monks, I have a Perl script that creates a list of URLs and formats them as hyperlinks for display, problem is the original array address is the first value; how do I get rid of it? Here is the code:
my $jobdirectory = "../jobs/"; my $linklist = joblist($jobdirectory); foreach my $jobInDirectory (@$linklist) { next unless ($jobInDirectory =~ m/\.html$/); $linklist = $linklist . '<a href="http://post/jobs/' . $jobInDirec +tory . '">' . $jobInDirectory . '</a>' . '<br />'; } open (LISTING, '>> ../jobs/index.html'); print LISTING $cgi->start_html( -title => 'Job Postings') . '<div id="main">' . $linklist . '</div>' . $cgi->end_html . "\n";
Here is the problem part of the output:
ARRAY(0x8187600)SP_HT_54.html SP_HT_59.html SP_HT_53.html SP_HT_52.html SP_HT_55.html
It's all good, with the exception of that unwanted "ARRAY(0x8187600)".
Thanks and obeisances,
-t.b.b.

Replies are listed 'Best First'.
Re: Array address returned as part of list
by kennethk (Abbot) on Nov 16, 2010 at 19:17 UTC
    The output you are seeing is a stringified array reference (see The Rest in perlreftut). The issue happens because you first put an array reference in the same variable you are concatenating into. You could fix this by using two variables, for example:

    my $jobdirectory = "../jobs/"; my $linklist = joblist($jobdirectory); my $output = ''; foreach my $jobInDirectory (@$linklist) { next unless ($jobInDirectory =~ m/\.html$/); $output .= '<a href="http://post/jobs/' . $jobInDirectory . '">' . + $jobInDirectory . '</a>' . '<br />'; } open (LISTING, '>> ../jobs/index.html'); print LISTING $cgi->start_html( -title => 'Job Postings') . '<div id="main">' . $output . '</div>' . $cgi->end_html . "\n";

    Note as well I changed your concatenation into a compound assignment ($x .= in place of $x = $x .). See Assignment Operators in perlop.

      kennethk,
      You nailed it dude, er, honorable monk!

      Thank you! (and thanks for the .= tip for concats :)
      -t.b.b.
Re: Array address returned as part of list
by ikegami (Patriarch) on Nov 16, 2010 at 19:18 UTC
    It looks like $linklist->[0] contains the string "ARRAY(0x8187600)SP_HT_54.html". You didn't show where you build @$linklist, so that's as far as we can go.