#!/usr/bin/perl # Description: Find largest numbers in array # # Last Update: 15 Oct 2010 # Designed by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # Coded by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # # Copyright 2006-2015 Dusan Baljevic # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . my @myarr = qw( 1.78 22.04 44 55.703 63 2 10.2 23 4 505 54 8 12 7 9 29 19 77.84 47 2059 3.88 ); my $LN = 3; my @SEENARR = (); # Splice array by $LN elements # my @listarr = splice(@myarr, 0, $LN); print "TASK: Find $LN largest numbers in \"@myarr\"\n"; for ( $LN .. $#myarr ) { my $numck = $myarr[$_]; if ( ! grep(/\b$numck\b/, @SEENARR) ) { if ( $numck > $l[2] ) { @l = ( $listarr[1], $listarr[2], $numck ) } elsif ( $numck > $listarr[1] ) { @listarr = ( $listarr[1], $numck, $listarr[2] ) } elsif ( $numck > $listarr[0] ) { @listarr = ( $numck, $listarr[1], $listarr[2] ) } } push(@SEENARR, $numck); } my @highest = (sort { $b <=> $a } @myarr)[0 .. $LN-1]; print "\nSOLUTION: @highest\n";