How would one go about finding the greatest number in an array of numbers using loops and if statements? For example, if you had an array like this:
(4, 8, 3, 19, 6, 11)
How would one go about finding the greatest number in an array of numbers using loops and if statements? For example, if you had an array like this:
(4, 8, 3, 19, 6, 11)
Try this,
my @nums = (2.1,1,3,10.1,10.2, 5);
my @sort_nums = sort {$a <=> $b} @nums;
my $grNum = pop @sort_nums;
print "Greatest Number is: ",$grNum;
Thxz balaji_red83! It working.
I changed a bit to make the result nicer:
my @nums = (2.1,1,3,10.1,10.2, 5);
my @sort_nums = sort {$a <=> $b} @nums;
my $grNum = pop @sort_nums;
print "Greatest Number is: $grNum\n"
~D~
If you just want the greatest number use an array slice to return only that number:
my @nums = (2.1,1,3,10.1,10.2, 5);
my $grNum = (sort {$a <=> $b} @nums)[-1];
print "Greatest Number is: $grNum\n"