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

Click For Summary

Discussion Overview

The discussion revolves around finding the largest numeric value in an array using Perl. Participants explore various methods and approaches to achieve this, including both simple comparisons and iterative techniques.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested, Homework-related

Main Points Raised

  • One participant asks how to return the largest numeric value in an array in Perl.
  • Another participant provides a subroutine that uses a pop operation and iterates through the array to find the maximum value.
  • A different approach is suggested that uses a temporary variable and a map function to filter and find the largest number, with a check for numeric values.
  • One participant emphasizes the importance of iterating through the array and updating a variable to track the largest number seen.
  • Another participant shares a simplified method for comparing three specific values, acknowledging that a more robust method would be better for larger arrays.
  • A follow-up comment questions the syntax used in a comparison, suggesting a clarification regarding logical conditions in Perl.
  • A later reply confirms the correctness of the previous suggestion after testing it with numbers.

Areas of Agreement / Disagreement

Participants present multiple approaches and methods without reaching a consensus on a single best solution. There is acknowledgment of different techniques suitable for varying array sizes and contexts.

Contextual Notes

Some methods rely on specific assumptions about the input data, such as the presence of numeric values, and there are unresolved details regarding Perl syntax and logical comparisons.

Who May Find This Useful

Individuals interested in Perl programming, particularly those looking to manipulate arrays and find maximum values, may find this discussion beneficial.

Monique
Staff Emeritus
Science Advisor
Gold Member
Messages
4,229
Reaction score
61
How do you return the largest numeric value in an array, in Perl?
 
Computer science news on Phys.org
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:
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;
 
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.
 
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 :)
 
$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)
 
Hey, thanks! I tested it with some numbers and you're right :blushing:
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
Replies
43
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
3K