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
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Eliminate single blank lines

Posted by TedH (TedH), 10 October 2004
Ah, the joys of Perl....

I've put together an address book in Perl and everything works fine.

A friend wants to use it for his church and be able to email people on distribution lists. So I'm putting together some mail out scripts for him.

Each record can have as many as 3 different email addresses. These all work well and send out the mail to respective lists. However he also would like to send to every mail address as well! When it prints out the email addresses there are blank lines because not every record would have 3 different emails. SendMail does not like blank lines.

How do I eliminate the blank lines when printing out the mailing list?

I always seem to end up biting off more than I can chew From what I gather this involves a regular expression with pattern matching in the middle of two operations (printing to screen and printing to a file) - I think.

Hope someone can help, cheers - Ted

maildata.txt (as is)


maildata.txt (should be)


This is the code which extracts the data and writes to screen and maildata.txt (I want to use this code, so the fix would need to work with it).

if ($input{'act'} eq "search") {
open (BOOK, "$database") || do {&no_open;};
open (MLST, ">$mailist") || die ("Could not open file. $!");
print <<"EOF";
<b> Search results for $input{'keyword'}: </b>

@sorted = sort(<BOOK>);
foreach $pair (@sorted)
if ($pair =~ /$input{'keyword'}/gi) {
    @input = split(/\|/, $pair);

     print "$h<br>$m<br>$s<br>\n";   ## prints to screen

     print MLST "$h\n$m\n$s\n";      ## prints to file for SendMail

PS: Got your magazine Graham - thanks

Posted by admin (Graham Ellis), 10 October 2004
First reaction - and *slightly* off topic - be very VERY careful about emailing a lot of people from a script.  Make sure you have everyone's permission, that you don't flood them, and that you don't email in such a way that everyone's getting the whole cc list.  OK?

Now - to answer your question ... I've copied and pasted it onto my computer - I'm writing from a wireless hot spot in a smokey bar on a cruise ship and it's not condusive to easy thinking;  I'll look overnight and post in the morning.

Posted by TedH (TedH), 10 October 2004
Not to worry about that, it's a closed list in a Methodist church in North Carolina. I'm not posting it on the site - did that once before and a major virus got spread using the script (had many thousands of returned 'hot' emails that I never sent - forgot to remove my email address after testing).

Enjoy the cruise.

Posted by admin (Graham Ellis), 11 October 2004
I just knew it would be a short answer early in the morning!


print MLST "$h\n$m\n$s\n"; ## prints to file for SendMail


foreach ($possible) ($h, $m, $s) {
       print MLST "$possible\n" if ($possible);

Yes, we will enjoy the cruise all about it on The Horses' Mouth at

Posted by TedH (TedH), 11 October 2004
Must have been some night Graham.....

 foreach ($possible) ($h, $m, $s) {
       print MLST "$possible\n" if ($possible);

gave error:
syntax error at c:\BWS\APACHE\CGI-BIN\BOBS\DISTA.PL line 106, near ") ("

tried redoing this into correct foreach statement syntax but still came up with blank lines.

maybe the sea's a bit ruff

Posted by admin (Graham Ellis), 11 October 2004
Think I have one set brackets too many. Will test it after lectures - listening to Brian Aker and Monty Widenius talking about MySQL at the moment  

Posted by TedH (TedH), 11 October 2004
Found this bit of code which works fine, long winded, but does the job.

# Remove blank lines (if any).
my @mlist;
open (MLST,"+<$mailist") || "Cannot open file";
 push @mlist,$_ unless ($_ eq "\n");
seek (MLST,0,0) || die "Cannot seek";
print MLST @mlist;
truncate (MLST,tell(MLST)) || die "Cannot truncate file";
close MLST || die "Cannot close file";

There's probably several ways to do this (usually is in Perl) some short, some long. I had various tries but some worked different than others and most didn't work at all. Did a good job of crashing Perl tho'.

MYSQL should be fun, I'm still struggling with flat-files - but gaining an understanding of these things.

I need a Photoshop break - gotta do something creative

Posted by admin (Graham Ellis), 11 October 2004
OK .. glad you have it fixed (and if it ain't bust, don't fix it). However, I did check back my example and found that, yes, I had put in one extra set of brackets.    Here's corrected code with a test piece too.

$h = "";
$m = "";
$s = "";
open (MLST,">FGHJK");

foreach $possible ($h, $m, $s) {
       print MLST "$possible\n" if ($possible);

I will comment - your code isn't - err - very efficient, nor short. So for other readers, perhaps it's not the ideal model for them to follow.

Posted by TedH (TedH), 11 October 2004
Works a treat Graham.

Much more efficient (pay attention here readers).

Thank you very much.

I assume there's plenty of booze on the ship <grin>

- Ted

Posted by Custard (Custard), 12 October 2004
Theres a hundred ways of doing this, and being a great believer in using the tools available...

Use egrep to filter the file on the way in...
my $fh = new FileHandle( 'cat maildata.txt | egrep -v ^$|' );

(A bit OT)

Or in perl...
my $fh = new FileHandle( '<maildata.txt' );
   while ( <$fh> ) {
   next if /^\s*$/;

More or less grahams solution..

But it looks like you already 'slurped' the file in using...
@sorted = sort(<BOOK>);

So why not...
@sorted = sort(grep( !/^\s*$/, <BOOK>));

Which gets rid of all the blank lines?
And catches blank lines that may have spaces in. (unlikely I know);

Or have I missed the point?

(I was a bit confused with the very long split line)


Ok, I did miss the point. I reread it again (properly this time) and BOOK isn't the maillist file is it?
(Slap on the wrist!)

Posted by TedH (TedH), 12 October 2004
Hundred ways.....thanks Custard  

While that makes Perl very flexible, it also creates problems in learning it. As I'm not really a programmer, but someone who does this out of necessity with the results of "will you do this for me...", it can get a bit confusing sometimes. However I am getting there (becuz the clients are slow at the moment).

I'll keep these and play around with them. I didn't want to wipe 'all' the blank lines becuz of the one at the end of the file, but it could come in useful.

Thanks - Ted

Posted by TedH (TedH), 12 October 2004
forgot to mention I'll have to find out more about egrep - cheers

Posted by Custard (Custard), 12 October 2004

I know. When I read your post I scanned through it and saw the title and the list of addresses and assumed that you just wanted to clean up the file.
Serves me right

And you're right, Perl's syntax is a whopper! (Perl6's is a double whopper)
If you wanted simple syntax, use Java or Smalltalk. Perl prefers a more syntax heavy but flexible approach to writing code. But it also can help people coming from other backgrounds, like shell or C.  You can always tell what type of programmer wrote a program in perl by looking at a 'for' loop

egrep is like grep but can also match on regular expressions.
-v negates the expression so matches all lines that the expression evaluates false for. Quite a lot to learn, 'man egrep' and 'man regex' are good on my Solaris box.

Take care.

This page is a thread posted to the opentalk forum at 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., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho