Mrtlc wrote:
> 
> I wrote the following script as a windows tail + count function,
> but it's too slow if the input is huge, how can it be improved?
> 
> if (!$ARGV[0]){
>     die("you've forgotten to enter the file name\n");
> }
> if (!$ARGV[1]) {
>     $n = 9;           # output 10 rows by default
> }
> else {
>     $n = $ARGV[1]-1;
> }
> 
> open IN, "$ARGV[0]" or die;
> 
> @_ = <IN>;
> 
> foreach my $i(($#_-$n)..$#_){
>     print $_[$i];
> }
> 
> close IN;
> 
> $i = $#_+1;
> printf "-------\nTotal # of rows: $i\n";


A more Perlish way to do it:

use warnings;
use strict;

@ARGV == 2 and my $n = pop || 10;
$n--;
@ARGV or die "you've forgotten to enter the file name\n";

my @tail;
while ( <> ) {
    push @tail, $_;
    shift @tail if @tail > $n;
    }
print @tail;
print "-------\nTotal # of rows: $.\n";

__END__


John
-- 
use Perl;
program
fulfillment

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

Reply via email to