Perl. Locate position of maximum value in an array

In summary, Perl is a powerful and versatile programming language that is often used for web development, system administration, and data analysis. One of its useful functions is being able to locate the position of the maximum value in an array, making it easier to manipulate data and perform calculations. Its syntax is similar to C and it has a large library of modules and tools available for use. Perl's flexibility and efficiency make it a popular choice for many developers and programmers.
  • #1
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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:
  • #6
Hi guys.
Thx ALOT! 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..
 
  • #7
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:

1. What is Perl?

Perl is a high-level, interpreted programming language commonly used for text manipulation, web development, and system administration.

2. How do I locate the position of the maximum value in an array using Perl?

To locate the position of the maximum value in an array, you can use the "max" function from the List::Util module. This function will return the maximum value in an array and its corresponding index position.

3. Can I use a built-in function to find the position of the maximum value in an array?

Yes, as mentioned before, the "max" function from the List::Util module is a built-in function in Perl that can be used to find the position of the maximum value in an array.

4. How does the "max" function work in Perl?

The "max" function works by iterating through the elements of an array and comparing them to find the largest value. It then returns the maximum value and its index position.

5. Are there alternative ways to locate the position of the maximum value in an array using Perl?

Yes, aside from using the "max" function, you can also use the "sort" function to sort the array in descending order and then retrieve the first element, which will be the maximum value and its index position. You can also use a loop to iterate through the array and keep track of the maximum value and its index position.

Similar threads

  • Programming and Computer Science
Replies
6
Views
10K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
24
Views
1K
  • Introductory Physics Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Introductory Physics Homework Help
Replies
10
Views
896
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top