Johann Snyman wrote:
> 
> Hello there,

Hello,

> I am a newby trying to learn pearl. I wrote my first little script,
> and I would love to se where I could have done things better (in a
> more proffesional way).
> 
> Feedback would be appreciated!
> 
> #!/usr/bin/perl
> use strict;
> use warnings;

Very good start.

> our @scores = `cat /home/johann/smail/Spam/* | grep ^Subject:` ;

Using backticks is fine in some cases and this is probably one of them. 
However you can use perl's built-in grep instead of the external grep
command.

our @scores = grep /^Subject:/, `cat /home/johann/smail/Spam/*`;


> # string looks like this: Subject: 10.20 Re: Hallo
> 
> our @vals;
> our $counter = 0;
> 
> for (@scores) {
>     $_ =~ /([0-9][0-9]\.[0-9][0-9])/;
>     $vals[$counter] = $1;

You should always verify that the match was successful before using the
numerical variables.  Also the usual way to add elements to an array is
with push().

    push @vals, $1 if /(\d\d\.\d\d)/;


>     $counter++;
> }

However, you can avoid using external commands through backticks and
populate @vals a little more directly:

@ARGV = glob '/home/johann/smail/Spam/*';
our @vals = map /^Subject:.*?(\d\d\.\d\d)/, <>;


> my $saved = `cat /home/johann/.spamscore` || die $!;
> chomp($saved);
> push @vals, $saved;
> 
> @scores = sort(@vals);

Since you are sorting numbers you should probably do a numerical sort.

@scores = sort { $a <=> $b } @vals;


>     #for (@scores) {
>     #       print $_,"\n";
>     #             }
> 
> 
> my $highest = @scores;
> $highest -= 1;
> 
> open (FH, ">/home/johann/.spamscore") || die $!;
> 
> print FH $scores[$highest];
> 
> close(FH);

You are reading from the file using `cat` but since you are also writing
to the same file it would make more sense to open the file once in
read/write mode:

use Fcntl qw(:seek);

my $scorefile = '/home/johann/.spamscore';

open my $fh, '+<', $scorefile or die "Cannot open $scorefile: $!";
# you may want to lock the file here

chomp( my $highest = <$fh> );

# reset the file for writing
seek $fh, 0, SEEK_SET or die "Cannot seek $scorefile: $!";
truncate $fh, 0 or die "Cannot truncate $scorefile: $!";

# this is more efficient then sorting
$highest < $_ and $highest = $_ for @vals;             

print $fh $highest;

close $fh;

__END__



John
-- 
use Perl;
program
fulfillment

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

Reply via email to