Why is print statement not printing NEW LINE (\n)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patelxxx
    New Member
    • May 2007
    • 135

    Why is print statement not printing NEW LINE (\n)

    When I running the following .cgi script, not sure why each print statement is not printing on a new line, even after entering \n. Browser=IE6

    See Perl code below:

    [CODE=Perl]#!C:/perl/bin/perl.exe -w

    print "Content-type:text/html\n\n";

    use CGI qw(:standard);

    $name = param('name');
    $age = param('age');
    $home = param('home');

    print "NAME: $name\n";
    print "HOME: $home\n";
    print "AGE: $age\n";[/CODE]

    The output I get is all on one line:
    NAME: fred HOME: england AGE: 42
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    it is in the source code, but html has no concept of a newline, you have to use html code to format html pages.

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Originally posted by patelxxx
      When I running the following .cgi script, not sure why each print statement is not printing on a new line, even after entering \n. Browser=IE6

      See Perl code below:

      [CODE=Perl]#!C:/perl/bin/perl.exe -w

      print "Content-type:text/html\n\n";

      use CGI qw(:standard);

      $name = param('name');
      $age = param('age');
      $home = param('home');

      print "NAME: $name\n";
      print "HOME: $home\n";
      print "AGE: $age\n";[/CODE]

      The output I get is all on one line:
      NAME: fred HOME: england AGE: 42
      I am going to guess that this script is called from another script because I don't see the HTML in here for the form.

      Either way, I went and set the variables (all 3) to values, and it printed just fine on 3 different lines.

      Not sure why it would be doing that to you.

      Regards,

      Jeff

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        He's viewing the output in a browser. He needs to use HTML code to format the output.

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by KevinADC
          He's viewing the output in a browser. He needs to use HTML code to format the output.
          he he, silly me. Wasn't thinking along the lines of displaying in the browser.

          Comment

          • patelxxx
            New Member
            • May 2007
            • 135

            #6
            Thank you Kevin, formatting the code with HTML worked. Thanks again.

            Kevin, why did the <p> tag before \n did the trick - allow me to print on seperate lines?

            i.e.

            [CODE:Perl]print "NAME: $name<p>\n";
            print "HOME: $home<p>\n";
            print "AGE: $age<p>\n";[/CODE]

            Thanks
            Last edited by patelxxx; Sep 17 '07, 08:06 PM. Reason: placed tags around the code

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              because <p> means a new paragraph in html code. The browser sees the html tags and formats the display accordingly. The browser completely ignores the newlines.

              You will need to learn some basic html coding if you are going to write CGI scripts. HTML is very very easy stuff. There are many online resources that can show you the basics of html code. HTML gets a bit more complicated when you use style sheets, refered to as CSS, but it's still fairly easy stuff.

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Originally posted by KevinADC
                because <p> means a new paragraph in html code. The browser sees the html tags and formats the display accordingly. The browser completely ignores the newlines.

                You will need to learn some basic html coding if you are going to write CGI scripts. HTML is very very easy stuff. There are many online resources that can show you the basics of html code. HTML gets a bit more complicated when you use style sheets, refered to as CSS, but it's still fairly easy stuff.
                My recommendation would be to go to w3schools.com. Nothing like learning from those who wrote the spec(s).

                Regards,

                Jeff

                Comment

                • patelxxx
                  New Member
                  • May 2007
                  • 135

                  #9
                  Thank you guys for the advise.

                  Comment

                  • erik sam

                    #10
                    add a <br> after each line (i.e. print "NAME: $name\n"; <br> )

                    Comment

                    • markleavenworth
                      New Member
                      • Jun 2014
                      • 1

                      #11
                      Thanks for the help, guys! Now that I've got perl modules under my belt, it looks like I'm only 7 year behind the curve!

                      Comment

                      • manageknowledge
                        New Member
                        • Jun 2014
                        • 10

                        #12
                        No need to use "\n", just use "<br>".

                        Comment

                        Working...