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

I would like to know how to properly edit a script to the effect of if no data was entered in a form field in a table cell to print the output as a blank table cell. Example:

##if data was entered print "<td colspan=x>$xyz</td>\n"; ##if no data was entered print "<td colspan=x>&nbsp;</td>\n";

I have searched the web, but could not find anything on this and have little understanding of perl. Is there an easy fix to this other than to write a sub routine 41 times? All I want to do is if no data was entered, just leave a blank table...otherwise print the data in the table.

Replies are listed 'Best First'.
Re: lost wtih blank data
by GrandFather (Saint) on Dec 09, 2009 at 02:15 UTC

    You could:

    $xyz = '&nbsp;' if ! defined $xyz; print "<td colspan=x>$xyz</td>\n";

    but maybe there is more you want to tell us about your problem?


    True laziness is hard work

      A shorter version (for Perl 5.10 and later) with the same meaning would be this:

      $xyz //= '&nbsp;'; print "<td colspan=x>$xyz</td>\n";

      But thinking about it just reminds me that my vim syntax highlighter doesn't understand these new 5.10 features.


      @_=qw; Just another Perl hacker,; ;$_=q=print "@_"= and eval;

      Thank you for your help, I will try this. I bid you peace.

Re: lost wtih blank data
by afoken (Chancellor) on Dec 09, 2009 at 09:46 UTC

    You are trying to fix the problem the wrong way - the 1990s way. Browsers typically do not show table cells that do not have content. So the 1990s solution was to insert "invisible" content, like the non-breaking space &nbsp;. The correct and clean way to make empty table cells visible is to use CSS: empty-cells:show. To get an exception from that rule, i.e. standard behaviour, you would use empty-cells:hide.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: lost wtih blank data
by igoryonya (Pilgrim) on Dec 09, 2009 at 15:56 UTC
    You don't need to put any space in empty table cell, but if you want it to look the same as nonempty table cell, you'd have to define empty cell with CSS in your html head section or in an external css file.
    <style type="text/css"> table {empty-cells: show; } </style>