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

Hi all, I use perl to view a flat file using table in html but it really urly if some file is blank so i thinh i should replace a blank field with an n/a so if a comment1 field and comment2 field is blank then put n/a in those field but it does not work. Please help me, here is my stuppid code
open (DATA,"PAD.txt") || die ("Can't Open data File \n"); @data=<DATA>; close DATA; &header_response; $x=0; foreach $line (@data) { $x++; ($Router, $itemgroup, $itemid, $SFC, $LogOp, $timestamp, $ncdescr +iption, $nccode, $operator, $comment1, $comment2)=split(/\|/,$line); if (($comment1 =~ " ") && ($comment2 =~ " ")) { print "<TR><TD>$x</TD><TD>$Router</TD><TD>$itemgroup</TD><TD>$item +id</TD><TD>$SFC</TD><TD>$LogOp</TD><TD>$timestamp</TD><TD>$ncdescript +ion</TD><TD>$nccode</TD><TD>$operator</TD><TD>n/a</TD><TD>n/a</TD></T +R> \n"; } elsif (($comment1 != " ") && ($comment2 =~ " ")) { print "<TR><TD>$x</TD><TD>$Router</TD><TD>$itemgroup</TD><TD>$itemid</ +TD><TD>$SFC</TD><TD>$LogOp</TD><TD>$timestamp</TD><TD>$ncdescription< +/TD><TD>$nccode</TD><TD>$operator</TD><TD>$comment1</TD><TD>n/a</TD>< +/TR> \n"; } else { print "<TR><TD>$x</TD><TD>$Router</TD><TD>$itemgroup</TD><TD>$itemid</ +TD><TD>$SFC</TD><TD>$LogOp</TD><TD>$timestamp</TD><TD>$ncdescription< +/TD><TD>$nccode</TD><TD>$operator</TD><TD>$comment1</TD><TD>$comment2 +</TD></TR> \n"; } } &footer_response;

Replies are listed 'Best First'.
Re: Help ! New in perl and cgi
by Marza (Vicar) on May 21, 2002 at 20:13 UTC

    Perl and CGI together eh? Well that is a hard match to pull off if you are new to programming

    I would suggest looking through our tutorials. Many of the elder monks have written good stuff for doing things

    However, looking at your code. Well unless my dyslexia is kicking in; you have your "N/A"s without quotes.

    Start using "use strict" and "use warnings" they are your friends and will point out problems for you. Your NA issue would have been reported to you.

    You made this harder then it needs to be. Why not simply read in the line, then have:

    if ($comment1 eq " ") { $comment1 = "n/a"; } if ($comment2 eq " ") { $comment2 = "n/a"; }

    Then you can have 1 print statement

    Some friendly advice. Pick up either Learning Perl or Begining Perl. They should help you with some examples on doing things.

    As I said Learning CGI and Learning Perl is hard to do at the same time. You probably should have gotten comfortable with Perl before doing the CGI class. Good luck and don't get discouraged. Perl is a fun language!

      Hi Marza, I tried your code and it did not work but if i replace eq with =~
      if ($comment1 =~ " ") { $comment1 = "n/a"; } if ($comment2 =~ " ") { $comment2 = "n/a"; }
      Then it run but all of the comeent1 and comment2 are n/a . Do you know why ? Thanks for reading
        if ($comment1 =~ " ") { # reads: if comment1 *HAS* a space ... } if ($comment1 !~ " ") { # reads: if comment1 *DOESN'T HAVE* a space ... } if ($comment1 eq " ") { # reads: if comment1 *IS* a space ... } if ($comment1 ne " ") { # reads: if comment1 *IS NOT* a space ... } if ($comment1 == " ") { # reads: if comment1 *Numerically Equals* a sp +ace # since space == 0, # ==> same as # if($comment1 == 0){ ... } ... } if ($comment1 != " ") { # reads: *Numerical* not equal to... ... }
        Hope this helps,,,

        PS: As Marza suggested, the llama book is a good book.

        Update: Added the other cases.

Re: Help ! New in perl and cgi
by UberDragon13 (Acolyte) on May 22, 2002 at 02:51 UTC
    Hi and welcome to the Perl language. As was previously mentioned the first thing you should read about is use strict;. You can also find out more information about strict; at perldoc.com. Speaking of perldoc.com, its a GREAT resource for learning the commands and usage of Perl.

    I would also recommned you read about the CGI.pm module. If you plan on using perl for CGI purposes you will find this module will help you enourmously with security and formating.

    By the way, there is no such thing as a stupid question, don't be so hard on yourself, read the suggestions by others, ask questions in the CB (also known as the ChatterBox found on the right of all your pages (unless you turned it off in your settings). I have found perlmonks.org to be a valuable resource in perl and I know you will too! :)

    ~UberDragon13

    ============================================================
    ....Sometimes life can be as bitter as dragon tears. But whether dragon tears are
    bitter or sweet depends entirely on how each person perceives the taste....
    ============================================================