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

Hi! I have the following cgi script:
#!/agl/tools/perl/bin/perl my @a=qx (rsh -l sonaxisz 'source.login ; findbug -p | sort -u | %s se +rver); print "Content-type:text/html\n\n"; print "<html><body>"; print "<table>"; print "<tr><td>@a<td><tr>"; print "</table></body></html>";
in the @a array i want to put the values that come from the command. The problem is that i want to print each value from the array in a separate table cell but i can't! in this way @a looks like this @a=ABC and puts ABC in the same cell and i want to put A in a cell, B in other cel, and C in other cel! I've tried with $a[0], $a[1], $a[2] but it also writes ABC at each!
Can you help me? Thank you very much!

Janitored by ybiC: Replace literal [ and ] with &#091; and &$093; to avoid PM-linking.

Replies are listed 'Best First'.
Re: array problem
by katgirl (Hermit) on Sep 12, 2003 at 12:55 UTC
    @a = ABC isn't an array. Do you mean @a = ("A","B","C");? If so...

    #!/agl/tools/perl/bin/perl my @a=qx (rsh -l sonaxisz 'source.login ; findbug -p | sort -u | %s se +rver); print "Content-type:text/html\n\n"; print "<html><body><table>"; for(@a){ print "<tr><td>$_</td></tr>"; } print "</table></body></html>";
    the for loop takes each item in the list @a and applies the code within the loop - but you could use map instead:
    @a = ("A","B","C"); print "Content-type:text/html\n\n"; print "<html><body><table>"; print map("<tr><td>$_</td></tr>",@a); print "</table></body></html>";
    any good for you?
      It is a very good ideea but i am not sure if qx returns separate items for output! I relly don't think so! Do you know any quotelike operators that returns separate items for output! Thank you for your time!!

          Wait...qx will return a list of command output lines...if your pipeline outputs newline separated lines, then you'll get a list of the kind that you seem to want.

          Compare this code and its outputs with what you have done.

          my @a = map {s/\n//g ? $_ : ()} `netstat -an`; print "$_\n" for @a;
    Re: array problem
    by Abigail-II (Bishop) on Sep 12, 2003 at 12:57 UTC
      my @a=qx (rsh -l sonaxisz 'source.login ; findbug -p | sort -u | %s se +rver);

      Are you sure this works? I only see one quote.

      Having said that, the amount of elements you get in @a depends on the number of lines your command returns. Each line becomes a separate element. However, if you do

      print "<tr><td>@a<td><tr>";

      then you will get a single HTML table cell, no matter how many elements you have in your array - unless of course the array (or $") contains "<td>".

      Abigail

    Re: array problem
    by bart (Canon) on Sep 12, 2003 at 12:39 UTC
      First off: are you sure the qx() operator returns separate items for output?

      You haven't inserted anything between the array items (except the default spaces). You're supposed to insert "</td><td>" between the cells. The easiest way to get that effect is

      local $" = "</td><td>";

      p.s. You should fix your tags, you don't have closing "<td>" and "<tr>" tags, but opening tags instead. And you might reconsider escaping the special HTML characters in your data, especially "<" and "&".

        i typed wrong, i have closing operators but i am not sure if qx returns separate items for output! I relly don't think so! Do you know any quotelike operators that returns separate items for output! Thank you for your time!!
          Check out perlop, where it says about qx() (or backticks, ``, which most of us will be more familiar with):
          In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.
          By default, that is a newline. So the list returned contains one item per line. The question remains: does your command line return a multiline result? I'm not familiar with what you try to do, so I can't tell.

          You can easily test in two ways:

          • @a = qx(COMMANDLINE); print scalar @a;
          • $a = qx(COMMANDLINE); print "<plaintext>$a";
          The former will print the number of items, the latter will make the browser display the text as plaintext, line wrapping an all.

          I just hope that yo ucan tweak your command line so that you can get separate lines for the returned items. If not, you can set $/ to whatever string separates the data items.

    Re: array problem
    by Roger (Parson) on Sep 12, 2003 at 13:27 UTC
      Hi, why don't you try something like:
      my $return_value = `rsh ...`; # your command my @a = split /\s+/, $return_value; print "Content-type: text/html\n\n<html><body><table>\n"; foreach (@a) { print "<tr><td>$_</td></tr>\n" } print "</table></body></html>\n";
      Assuming that your rsh command returns a set of values separated by spaces. Change the split pattern if the output of the rsh uses a different separator.
    Re: array problem
    by TomDLux (Vicar) on Sep 13, 2003 at 16:31 UTC

      You seem totally confident concerning the value of @a, without presenting any reasons or evidence. How about running a little test script:

      #!/agl/tools/perl/bin/perl use strict; # Notice these two use warnings; # essential modifications! my @a=qx (rsh -l sonaxisz 'source.login ; findbug -p | sort -u | %s se +rver'); print '@a contains ' . scalar @a . "elements:\n", @a;\

      Betcha a mug of Trappist Ale you'll be surprised at the output.

      --
      TTTATCGGTCGTTATATAGATGTTTGCA