in reply to Passing Arrays to Subroutines

I am a true believer of abstracting your code into subroutines, but for this it just seems like a waste of time. Also, why bother writing the data back out to the same file? Just log the results to a new file. Anyways, the problem you are having is that you are not splitting each line. Since you didn't specify which email address you are using, i'll have to assume you meant all of them. If you know exactly which ones you want, just use split with an array slice:
open (FILE, '<', 'prev.txt') or die "Can't open prev.txt\n"; while (<FILE>) { my @email = (split('\s',$_))[1..3]; for (@email) { # send email # log results } }
If you don't know which fields will be email addresses, you can bring Email::Valid along for the ride:
use Email::Valid; open (FILE, '<', 'prev.txt') or die "Can't open prev.txt\n"; while (<FILE>) { my @stuff = split; for (@stuff) { next unless Email::Valid->address($_); # send email to $_ # log results } }
Finally, please consider using MIME::Lite to send the emails. It's really easy to use and it really rocks. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: Passing Arrays to Subroutines
by shemyaza (Novice) on Nov 26, 2003 at 16:36 UTC
    I take your point about the use of subroutines. Although the code is going to do something useful it is also helpling me learn perl so I was experimenting a bit. The written out file won't be quite the same as that read in as the last item of each line will be a flag indicating the level of warning emails that have been sent out. The date will change too (10 11 104 in my example). Looks like my problem is splitting on white space. I copied some code from the SAMS book Teach Yourself Perl in 24 Hours which wasn't too clear on whether it was reading whole lines or not. FYI I know which items will be email addresses. Cheers. S.