| ||||||||||||
| ||||||||||||
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 ![]() Hope someone can help, cheers - Ted maildata.txt (as is) ------------------- goofy@dada.com donald@dada.com fred@dada.com wilma@dada.com flintstones@dada.com elmer@dada.com ------------------- maildata.txt (should be) ------------------- goofy@dada.com donald@dada.com fred@dada.com wilma@dada.com flintstones@dada.com elmer@dada.com ------------------- 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> <br> EOF $count=0; @sorted = sort(<BOOK>); foreach $pair (@sorted) { if ($pair =~ /$input{'keyword'}/gi) { $count++; @input = split(/\|/, $pair); ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u,$v,$w,$x,$y,$z)=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 } close(BOOK); } close(MLST); 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!Replace print MLST "$h\n$m\n$s\n"; ## prints to file for SendMail By foreach ($possible) ($h, $m, $s) { print MLST "$possible\n" if ($possible); } Yes, we will enjoy the cruise ...read all about it on The Horses' Mouth at http://www.wellho.net/horse 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"; foreach(<MLST>){ 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.Code:
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... Code:
(A bit OT) Or in perl... Code:
More or less grahams solution.. But it looks like you already 'slurped' the file in using... Code:
So why not... Code:
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) B 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 - cheersPosted by Custard (Custard), 12 October 2004 Cheers, 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. B 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.
|
| |||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |