469,326 Members | 1,351 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

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

135 100+
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:

Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe -w
  2.  
  3. print "Content-type:text/html\n\n";
  4.  
  5. use CGI qw(:standard);
  6.  
  7. $name = param('name');
  8. $age = param('age');
  9. $home = param('home');
  10.  
  11. print "NAME: $name\n";
  12. print "HOME: $home\n";
  13. print "AGE:  $age\n";
The output I get is all on one line:
NAME: fred HOME: england AGE: 42
Sep 17 '07 #1
11 27672
KevinADC
4,059 Expert 2GB
it is in the source code, but html has no concept of a newline, you have to use html code to format html pages.
Sep 17 '07 #2
numberwhun
3,503 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe -w
  2.  
  3. print "Content-type:text/html\n\n";
  4.  
  5. use CGI qw(:standard);
  6.  
  7. $name = param('name');
  8. $age = param('age');
  9. $home = param('home');
  10.  
  11. print "NAME: $name\n";
  12. print "HOME: $home\n";
  13. print "AGE:  $age\n";
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
Sep 17 '07 #3
KevinADC
4,059 Expert 2GB
He's viewing the output in a browser. He needs to use HTML code to format the output.
Sep 17 '07 #4
numberwhun
3,503 Expert Mod 2GB
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.
Sep 17 '07 #5
patelxxx
135 100+
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
Sep 17 '07 #6
KevinADC
4,059 Expert 2GB
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.
Sep 17 '07 #7
numberwhun
3,503 Expert Mod 2GB
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
Sep 17 '07 #8
patelxxx
135 100+
Thank you guys for the advise.
Sep 18 '07 #9
add a <br> after each line (i.e. print "NAME: $name\n"; <br> )
Oct 7 '10 #10
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!
Jun 12 '14 #11
No need to use "\n", just use "<br>".
Jun 22 '14 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by entoone | last post: by
1 post views Thread by Michelle | last post: by
9 posts views Thread by François Pinard | last post: by
3 posts views Thread by no_spam_for_gman | last post: by
12 posts views Thread by jt2 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.