On Thu, 23 Jan 2003, Pam Derks wrote:
> Hi all, > > I want to grap the: > last first middle(if any) email > from a text file > > sometimes there is a middle intital, sometimes there isn't > > sample data: > Smith, Mary [EMAIL PROTECTED] > Jones, Tommy Lee [EMAIL PROTECTED] > > can someone suggest improvements to the code below? Does it always come in the same order?? If it does, then: I've also removed a trailing "," from all the names (if any) #!/usr/bin/perl -w my ($first, $middle, $last, $email); open(IN, "text.txt") or die("nope: $!\n"); while(my $ln = <IN>) { chomp $ln; ($last, $first, $email) = split / +/, $ln, 3; ($email, $middle) = reverse split(/ +/, $email); ($first, $middle, $last) = map { $_ = '' if (!defined $_); s/ *,$/; $_;} ($first, $middle, $last); print("first: $first\n"); print("last: $last\n"); print("middle: $middle\n"); print("email: $email\n"); } close IN; > > #!/usr/bin/perl -w > > my ($first, $last, $middle, @fields); > > open(IN, "text.txt") or die("nope: $!\n"); > while($line = <IN>){ > > chomp($line); > > @fields=split(/ /, $line); > #an array in scalar context returns number of elements > $number = @fields; > print("number: $number\n"); > > #if there is no remainder, there are 3 elements > if($number % 3 == 0){ > $first = $fields[0]; > $last = $fields[1]; > $email= $fields[2]; > } > > #if there is no remainder, there are 4 elements > elsif($number % 4 == 0){ > $first = $fields[0]; > $last = $fields[1]; > $middle= $fields[2]; > $email= $fields[3]; > } > > print("first: $first\n"); > print("last: $last\n"); > print("middle: $middle\n"); > print("email: $email\n"); > } > > close(IN); > > thanks in advance, > > Pam > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]