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

Hey all, I'm doing a little thing to modify some acl files and automatically email the affected users. I'm having trouble (and probably a newbie problem) ignoring the last line (undef) from an array. The following code is an example from a test program I wrote to perform the function before I write it into the main code, hence the print statement on the $user variable. I keep getting ".mydomain.com" as the last entry in the list, but I don't want to cause a bounced email everytime I run the program. I've tried adding an if statement to catch the undef line, but I must be doing it wrong. I'm sure this is an easy one for most. Can anyone help me? Thanks in advance, Bradley
#/usr/gnu/bin/perl -w use strict; my $file = "pathname/to/acl/user/file"; open (ACL,$file); my @users = <ACL>; close (ACL); my $user; foreach $user (@users) { chomp ($user); $user = "$user" . "\.mydomain\.com"; #add domain for smtp print $user; #to get a listing }

Replies are listed 'Best First'.
Re: undef line in an array
by voyager (Friar) on Jun 26, 2001 at 00:03 UTC
    You have an empty element you want to ignore? How about one of:
    @users = grep { $_ } @users; #eleminate undef elements
    or in your loop either of:
    next unless $user; #after the chomp (which could be done on the whole +array outside the loop print $user . '.mydomain.com' if $user;
Re: undef line in an array
by Beatnik (Parson) on Jun 26, 2001 at 00:05 UTC
    try something like
    #!usr/gnu/bin/perl -w use strict; my $file = "pathname/to/acl/user/file"; my $user; open (ACL,"<$file") || die $!; while($user = <ACL>) { chomp $user; next if !$user; $user = "$user.mydomain.com"; #add domain for smtp print $user; #to get a listing } close(ACL);
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Thanks guys. That did the trick. Bradley p.s. Beatnik... you have to tell me what the latin means. Where ever there is confusion to be had... I'll be there.