Hi Pam. Some peripheral comments, now that others have
done all of the hard work :)

John W. Krahn wrote:
> Pam Derks wrote:
>> I want to grap the:
>> last first middle(if any) email
>> from a text file
>>
>> sample data:
>> Smith, Mary [EMAIL PROTECTED]
>> Jones, Tommy Lee [EMAIL PROTECTED]
>>
>> can someone suggest improvements to the code below?
>>
>> #!/usr/bin/perl -w
>
> use strict;
>
>
>> my ($first, $last, $middle, @fields);
>
> You should define these variables in the smallest possible scope
> instead of at the top of the program.
>
>> open(IN, "text.txt") or die("nope: $!\n");

Putting a \n on the end of the 'die' string stops Perl from putting
the source line number and data input line number on the end.

    ... or die $!;

is better for debugging.

>> while($line = <IN>){
>
> while ( my $line = <IN> ) {

You might think about using:

    while (<IN>)

where the file records are placed in $_, which can be used as an
implicit parameter for most built-in functions.

>>         chomp($line);
>>
>>         @fields=split(/ /, $line);
>
> split / / will split using a single space, it is more efficient to use
> the default.
>
>           my @fields = split ' ', $line;

For instance, this would be:

    my @fields = split;

with the above form of while().

>>         #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){
>
> Why use modulus, just use == 3.
>
>           if ( @fields == 3 ) {
>
>
>>                 $first = $fields[0];
>>                 $last = $fields[1];
>>                 $email= $fields[2];

$first and $last are always fields [0] and [1] (whichever way round!,
and you can index from the end of an array with negative numbers,
so:

    $last = $fields[0];
    $first = $fields[1];
    $email = $fields[-1];

$middle is either field [2] or blank, depending on the size of the
array:

    $middle = (@fields > 3 ? $fields[2] : '');

Apart from that, everything I an think of has been said.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to