Perl. Locate position of maximum value in an array

Click For Summary

Discussion Overview

The discussion revolves around locating the maximum value and its position in an array using Perl. Participants explore various methods and algorithms for achieving this, while also comparing Perl to other programming languages like MATLAB, C, and Python. The conversation includes technical explanations, personal experiences, and opinions on the ease of use of different programming environments.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant shares a Perl code snippet to find the maximum value and its index, suggesting a straightforward algorithm that iterates through the array.
  • Another participant proposes using the List::Util module in Perl to access a built-in max function, noting that it does not directly address the need for locating the index.
  • Concerns are raised about the initial algorithm's applicability only to positive integers, with suggestions to avoid sorting the array to find the maximum value.
  • A participant expresses frustration with Perl's lack of advanced math libraries compared to MATLAB, while another counters that there are indeed advanced libraries available in Perl.
  • Some participants mention alternative programming languages like C, C++, and Python, suggesting they might be more suitable for certain tasks, especially for those familiar with MATLAB.
  • One participant shares their personal struggle with programming in C and C++, citing time constraints and a lack of experience, while expressing a desire to improve their skills.

Areas of Agreement / Disagreement

Participants express differing opinions on the ease of programming in Perl versus other languages, with some advocating for Perl's capabilities while others highlight its limitations. There is no consensus on the best approach to finding the maximum value and its index, as multiple methods and opinions are presented.

Contextual Notes

Participants mention the need for performance in handling large arrays (100,000 to 1 million elements) and the challenges associated with programming in different languages under tight deadlines. The discussion reflects varying levels of programming experience among participants.

Who May Find This Useful

This discussion may be useful for programmers looking for methods to find maximum values in arrays using Perl, as well as those comparing programming languages for mathematical tasks.

Don Carnage
Hi guys.
I need to locate the maximum value in an array in Perl. So let's say it looks as follows:

# Define array
my @a;

# Define inputs in array
a[1]=11;
a[2]=12;
a[3]=13;
a[4]=14;
a[5]=15;
a[6]=13;
a[7]=12;
a[8]=14;

Then I would like to do something like max[@a] given the result "15" and then
maxloc[@a] given the result "5"

The arrays I use have about 100,000-1million elements.

Hope you guys can help..
Peter.

.. and the worst thing is that this is so easy in MATLAB. Damn I hate that the MATLAB license is so expensive!
 
Technology news on Phys.org
Hello,

".. and the worst thing is that this is so easy in MATLAB. Damn I hate that the MATLAB license is so expensive! "

If you can use any language or environment you want, why not use C, C++, or Java? They all have very similar syntax and semantics to MATLAB.

Here's the algorithm to do what you're asking:


Max( array[1...n] : array of integers )

max_value ← 0
max_index ← 0

for i ← 1...n
. . . if array > max_value then
. . . . . . max_value ← array
. . . . . . max_index ← i

return (max_value, max_index)

This is easy to implement in C, C++, Java, and MATLAB. It's easy to implement in PERL, too.

$max_value = 0;
$max_index = 0;

for($i = 0; $i < n;$i ++)
{
. . . if($array > $max_value)
. . . {
. . . . . . $max_index = $i;
. . . . . . $max_value = $array;
. . . }
}

return $max_index; # or $max_value, or an array with both in either order.
 
sub max { @_ = sort { $a <=> $b } @_; pop }

More reasonably, try putting this at the top of your file:

use List::Util qw(max);

This will access the standard perl module "List::Util" and import the method "max", which returns the largest numerical item in a list. There are several other methods in that module you may find useful if you look, including sum, and reduce (foldl). There is a large body of perl modules, including some very useful ones installed by default, and if you're going to be using perl it pays to learn about some of the basic ones.

EDIT: I do note these won't give you the "maxloc" type function you were asking for. However AUMathTutor's method will.
 
Last edited:
Actually, the algorithm I gave only works for positive integers in the array. It can easily be changed, though.

And you don't want to sort it to get the max... though library functions are good to look into.
 
AUMathTutor said:
And you don't want to sort it to get the max...

I think there are several reasons one does not want to use my code sample :)
 
Last edited:
Hi guys.
Thx a lot! I will try this..

I would love to use C++ instead of Perl! The problem is just that I have to make a script at my work where we have some pretty massive deadlines, and every time I try to do something in C,C++ or FORTRAN I always get my boss on my back because it takes so long. There is always some issue with allocation or something else. Guess the biggest problem is that I am NOT a programmer, but fortunately I will get a Course in C++ in some months so I can finally get a good understanding of pointers and so on. The thing that irritates me the most with Perl is, that there is really no advances math libraries available.. Anyways, life just sucks without MATLAB if you have to do a lot of productive WORK! MATLAB is just 10 times speed of everything else when doing advanced mathematical work IMHO..
 
Don Carnage said:
The thing that irritates me the most with Perl is, that there is really no advances math libraries available..

Are you quite sure? I'd imagine there are a lot of advanced math libraries for perl. What would consider to constitute an "advanced math library"?

If you want to check, the perl module repository is here:

http://search.cpan.org/

I see there's a module for LAPACK, for example.

If it's not essential to stick to Perl specifically I've also previously heard it suggested that people familiar with Matlab are often comfortable using "numpy", or "Numerical Python", which is the Python scripting language packaged with some advanced-math and plotting libraries. There's also something called Octave, which is a free/GNU application similar to Matlab in many ways.
 
Last edited:

Similar threads

  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
24
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
8K