Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Odd linebreak

Posted by TedH (TedH), 7 March 2008
Hi, when running some code result through an HTML validator I discovered a linebreak that shouldn't be there. I've looked at this from every angle I can possibly think of and can find no obvious reason for this happening.

Code:
 foreach $arcdir (@lines) {
 ## Monthnames
 @months = qw(January February March April May June July August September October November December);
 $aa = substr($arcdir, 0, 4);
 $bb = substr($arcdir, 4, 6);
 $macvm = $months[$bb-1];
 $macvy = $aa;
print qq ~&nbsp;<a href="$urlcgi/archive.pl?var1=$arcdir" onFocus="if(this.blur)this.blur()">$macvy</a>&nbsp;~;
}


The linebreak occurs in the HTML output like this:
Code:
<a href="/cgi-bin/news/archive.pl?var1=2008
">2008</a>


The arcdir variable is defined right in the foreach and looks okay in the substring. Is Perl doing something I am not aware of here?

Any ideas?
- Ted

PS
If I've mentioned something like this before and forgotten about it, put it down to age (silly grin)

Posted by george_Ball (george), 7 March 2008
The newline appears to be in the variable $arcdir, which is the loop control variable stepping through the array @lines.

Now I'm guessing that you have filled the array using the <> operator in array context:-

 @lines = <INPUT>;

or something like that. The < > operator leaves the newline on each of the lines that it reads, so you will probably have one of these "phantom" newlines here...

My suggestion is to use chomp() to remove the newline (if it's there) on the variable $arcdir before you use it to build the href in your <a> tag. Remember to that chomp() doesn't return the chomped string though, so you can't use the function call in an expression as you may expect. You'll have to do

...
chomp ($arcdir);
print qq ....
...

Hope that makes sense.



Posted by TedH (TedH), 7 March 2008
A great big THANK YOU George.

The validator returned no errors upon check. Worked a treat.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho