Finding the Largest Numeric Value in an Array: A Perl Solution

In summary, The conversation discusses how to return the largest numeric value in an array using Perl. The suggested method is to iterate through the array and update a variable with the largest number seen. The conversation also provides an alternative solution using conditional statements.
  • #1
Monique
Staff Emeritus
Science Advisor
Gold Member
4,219
67
How do you return the largest numeric value in an array, in Perl?
 
Computer science news on Phys.org
  • #2
Maybe this helps (I don't have any Perl skills, only Googling skills :smile: ).

sub max
{ my $max = pop(@_);
foreach (@_)
{ $max = $_ if $_ > $max;
}
$max;
}

http://www.codetoad.com/forum/18_23829.asp
 
Last edited by a moderator:
  • #3
Monique said:
How do you return the largest numeric value in an array, in Perl?
I don't think perl has a maximum function so you have to do something like
Code:
my $tmp='';
map {$tmp=$_ if ($_>$tmp and $_=~/^-?\d*.?\d*$/s and $_=~ /\d/}  @array
die("The array doesn't contain any numbers ") unless ($tmp=~ /\d/);
return $tmp;
 
  • #4
I always try to advise people by asking how they would do it.

If you had to find the largest number in a list of a million numbers, how would you do it?

You would probably go through it one by one, remembering the largest number you've seen.


So, that's how you should do it on the computer: you have a variable for storing the largest number you've seen, and you iterate through the array, updating this variable whenever you see a bigger number.
 
  • #5
Well, luckily I only needed to compare three values so I took the easy way out:

Code:
        if ($frame1>$frame2&&$frame3){
            $ORF=$frame1;
        } elsif ($frame2>$frame3){
            $ORF=$frame2;
        } else {
            $ORF=$frame3;
        }
I guess the way you suggest would be best for a large array, thanks :)
 
  • #6
$frame1>$frame2&&$frame3

I'm not familiar with Perl syntax, but don't you mean something like ($frame1>$frame2)&&($frame1>$frame3)? (In other languages, at least, the two are much different)
 
  • #7
Hey, thanks! I tested it with some numbers and you're right :blushing:
 

What is an array?

An array is a data structure that can store a collection of values in a single variable. It allows for easy storage and access of multiple values using a single identifier.

How do you find the largest numeric value in an array?

To find the largest numeric value in an array, you can use a loop to compare each element in the array to a temporary variable. If the element is larger than the variable, the variable is updated with that element's value. Once the loop is finished, the temporary variable will hold the largest numeric value in the array.

What if the array contains non-numeric values?

If the array contains non-numeric values, you can either remove them before finding the largest numeric value or use a conditional statement within the loop to skip over non-numeric values. Alternatively, you can use a built-in function like "array_filter" to remove non-numeric values before finding the largest numeric value.

Can an array have more than one largest numeric value?

Yes, an array can have more than one largest numeric value if there are multiple elements with the same largest value. In this case, the temporary variable used to find the largest value will only hold the first element with that value, so you may need to use a loop or built-in function to find all elements with the largest value.

Are there any built-in functions for finding the largest numeric value in an array?

Yes, there are built-in functions like "max" and "array_max" that can find the largest numeric value in an array. These functions also have additional features such as allowing for the comparison of multidimensional arrays and the ability to specify the data type of the array elements.

Similar threads

  • Computing and Technology
Replies
2
Views
724
  • Computing and Technology
Replies
4
Views
763
  • Programming and Computer Science
Replies
2
Views
1K
  • General Math
Replies
7
Views
857
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • General Discussion
Replies
5
Views
633
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
6
Views
812
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Programming and Computer Science
Replies
32
Views
2K
Back
Top