-1

I am reading some values from an SQLite database, based on a form

my $Fruits= $FORM{Fruits}; 
my $FruitsTable =0; 

Once I get the values from the database, they should be stored in a variable called my $SelectedFruits.

while ( my @column = $sth->fetchrow_array() ) {

    $FruitsTable = 1;

    # Within the table there is a row called Tropical which contains bananas, guaves and oranges. I want to select bananas and guaves
    my $SelectedFruits = print "<tr>
                           <td>$Tropical[0]</td>
                           <td>$Tropical[1]</td>
                     </tr>";
}

if they are found, a table with my selection should be printed (my problem is when the printing order):

if ( $FruitsTable == 1 ) {

    print "<table>
            <thead>
              <tr>
                <th>Bananas</th>
                 <th>Guave</th>
              </tr>
           </thead>
        <tbody>";

    $SelectedFruits;

    print "</tbody></table>";
}

The code exits without errors.

The problem here is that $bananas is printed first and the table's header and footer are printed afterwards, so the table looks crazy.

How can I print the table's header first, then the $bananas and then the table's footer?

Chüngel
  • 375
  • 4
  • 19
  • You probably believe you are simplifying the code for us by using `bananas` all over the place, but in fact you have made it more awkward to read. When *every* variable contains `bananas` it doesn't provide any information, and should be deleted everywhere. – Borodin Jan 26 '17 at 11:50

1 Answers1

3

You must always use strict and use warnings 'all' at the top of every Perl program that you write. It would have been a lot of help to you with this question

The problem is that this

my $bananas = print "<tr>
    <td>$happyBananas[0]</td>
    <td>$hippieBananas[1]</td>
</tr>";

will immediately print the table row, set the lexical variable $bananas to 1 (the return code of print) and then throw it away at the end of the loop

You want something like this instead, which accumulates the contents of the table into non-local variable $bananas_rows instead of printing it, and can then also be used as the flag to determine whether there is a table to print

Note that I've used "here documents" which rather mess up the indentation, but are more readable than bare double quotes

my $bananas_rows; # Don't initialise variables without a reason

while ( my @column = $sth->fetchrow_array() ) {

    $bananas_rows .= <<END_HTML;
<tr>
    <td>$happyBananas[0]</td>
    <td>$hippieBananas[1]</td>
</tr>
END_HTML

}

  if ( $bananas_rows == 1 ) {

   print <<END_HTML;
<table>
  <thead>
    <tr>
      <th>Happy Bananas</th>
      <th>Hippie Bananas</th>
    </tr>
  </thead>
  <tbody>
END_HTML

    print $bananas_rows;

    print "</tbody></table>"; 

}
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Hi @Borodin, thanks, I'm learning! I'm having an error now "Can't find string terminator "END_HTML" anywhere before EOF" am I missing something? – Chüngel Jan 26 '17 at 14:46
  • @Chüngel: Make sure you don't have a space at the end of `END_HTML` – Toto Jan 26 '17 at 14:53
  • @Chüngel: Yes, there must be no spaces before or after `END_HTML`. It's a restriction that should be relaxed on near-future Perl 5 releases. – Borodin Jan 26 '17 at 14:56
  • Thanks guys, double quotes mentioned in this post: [link] (http://stackoverflow.com/questions/37383898/getting-error-in-perl-cant-find-string-terminator-eof-anywhere-before-eof-at) + this post + the spaces solved the issue – Chüngel Jan 26 '17 at 15:26
  • @Chüngel: I'm pleased you have sorted it out. `print << "XXX"` is the same as `print << – Borodin Jan 26 '17 at 15:40