shemyaza has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have started working on my first real project, to send reminder emails out when meetings are due. Ultimately I want to read a text file (prev.txt) which consists of lines with 9 entries into an array, for each set of 9 entries process them and send emails as necessary, then write the amended array to a file again.

After an earlier query I have altered my first snippet of code as below and put the print command in to test what I have got but it doesn't work. If I replace the 2 with 0 I get what looks like the first line of the data file printed but I expected to get just the first item, 31307MWB using the example below.

A line of data looks like:-

31307MWB name@address name@address name@address 10 11 103 90 0

Can someone tell me what I am doing wrong?

TIA S.

#!/usr/bin/perl -w use strict; my(@DATAFILE); @DATAFILE=&readdata(); print $DATAFILE[2]; &writedata(@DATAFILE); # # Subroutine for reading prev.txt into an array # sub readdata { open (PH, "prev.txt") || die "Cannot open prev.txt: $!"; my(@DATA)=<PH>; chomp @DATA; close (PH); return(@DATA); } # # Subroutine for writing array into prev.txt # sub writedata { my(@DATA)=@_; open (PH, ">prev.txt") || die "Cannot open prev.txt: $!"; foreach(@DATA) { print PH "$_\n"; } close (PH); }

Replies are listed 'Best First'.
Re: Passing Arrays to Subroutines
by jeffa (Bishop) on Nov 26, 2003 at 13:38 UTC
    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)
    
      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.
Re: Passing Arrays to Subroutines
by duff (Parson) on Nov 26, 2003 at 14:56 UTC

    You're not paying attention to what you've asked perl to do. You say:

    If I replace the 2 with 0 I get what looks like the first line of the data file printed but I expected to get just the first item
    Yet, looking at your code, you read all of the lines of your file into an array, then return that array from your readdata() subroutine. In your main bit of code, you print out the 3rd item in that array of lines. So, of course you're going to get an entire line of data!

    What you apparently want to do is subsequently split each line on whitespace (maybe?) and then print out the bits of the line that interest you.

    ... my @lines = readdata(); foreach my $line (@lines) { my @fields = split ' ', $line; print $fields[2]; } ...

    It would greatly help your cognitive processes if you chose better names for your subroutines and variables; "data" is just too generic. Use the variable names to describe what the data are.

      Thanks for your help. I copied part of the code from a book which wasn't too clear on whether the array items would be split on whitespace or be lines. I see now that they are lines. What you have suggested is pretty much what I had in mind. I take your point about variable names, I guess I wasn't being very imaginative :-). Cheers S.
        If you are developing generic subroutines, then the names inside your subroutines aren't bad, but the fact that you hard code the name of the file is. If you pass the in the name of the file as an argument to the sub, then you will have some reusable subroutines:
        sub file_to_list { my $name = shift or die "must have name to read"; open (FILE, '<', $name) or die "can't read from $name: $!"; chomp(my @data = <FILE>); return @data; } sub list_to_file { my $name = shift or die "must have name to write"; open (FILE, '>', $name) or die "can't write to $name: $!"; print FILE "$_\n" for @_; } my @foo = file_to_list('file.in'); print "$_\n" for @foo; list_to_file('file.out',@foo);

        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)
        
Re: Passing Arrays to Subroutines
by Abigail-II (Bishop) on Nov 26, 2003 at 13:38 UTC
    You say, it doesn't work. I cut and paste the code, and I say it works.

    Now what? Perhaps you could explain your definition of "it doesn't work"?

    Abigail

      Thanks for your message. I see now that item 0 is line 1 of the data file and item 1 is line 2 etc etc. Looks like I need to split on white space to achieve what I want where item 0 is '31307MWB', item 2 is 'name@address' etc etc. Either that or I can change my plan a little and work with the lines. Cheers S.