code for find out nth larget number in an array by using perl

Posted by user136104 on Stack Overflow See other posts from Stack Overflow or by user136104
Published on 2010-03-25T08:37:10Z Indexed on 2010/03/25 8:43 UTC
Read the original article Hit count: 287

Filed under:

I have written following code in perl

#!/usr/bin/perl

@array =(3,6,8,1,2);

my $second_largest =0;

my $largest = 0;

for (@array)

{

   if($_ > $largest)

   {

       $second_largest = $largest;

       $largest = $_;

   }

   if($_ > $second_largest && $_ < $largest)

   {
        $second_largest = $_;
   }

}
print "Second largest::".$second_largest;

print "largest::".$largest;

But I need a general code to find out "Nth" largest and smallest number of an array

Plz help me

© Stack Overflow or respective owner

Related posts about perl