Jakob Kofoed wrote:
> 
> Hi,

Hello,

> I am trying to get the maximum number in a file looking like:
> 
> 1   5001
> 2   5002
> 3   5003
> 4   5004
> 5   5005
> 6   5006
> 7   5007
> 8   5008
> 9   5009
> 10   5010
> 11   5011
> 12   5012
> 13   5013
> 14   5014
> 15   5015
> 16   5016
> 17   5017
> 18   5018
> 19   5019
> 
> I had the idea to push the individual line in a row into a array and se
> the List::Util module - but I cant get it to work.
> 
> Here is what I reached - can you tell me where I go wrong?
> 
> #! /usr/bin/perl -w
> 
> use List::Util qw(max min);
> 
> open IN, "<", "num2.txt";
> my @in = <IN>;
> push @col1, $in[0];
> 
> print max @col1;
> close IN;


If the lines are always in numerical order then you could just read the
last line of the file.

use File::ReadBackwards;

my $bw = File::ReadBackwards->new( 'num2.txt' )
    or die "Cannot open num2.txt: $!";

my $max = 0 + $bw->readline;

print "Max: $max\n";


Or if you don't mind reading through the whole file.

open IN, '<', 'num2.txt' or die "Cannot open num2.txt: $!";

my $max = 0;
while ( <IN> ) {
    $max = 0 + $_ if $max < $_;
    }

close IN;

print "Max: $max\n";



John
-- 
use Perl;
program
fulfillment

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

Reply via email to