Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Perl. Locate position of maximum value in an array

  1. Apr 1, 2009 #1
    Hi guys.
    I need to locate the maximum value in an array in Perl. So lets say it looks as follows:

    # Define array
    my @a;

    # Define inputs in array

    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..

    .. and the worst thing is that this is so easy in MATLAB. Damn I hate that the MATLAB license is so expensive!!!
  2. jcsd
  3. Apr 1, 2009 #2

    ".. 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.
  4. Apr 1, 2009 #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: Apr 1, 2009
  5. Apr 1, 2009 #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.
  6. Apr 1, 2009 #5
    I think there are several reasons one does not want to use my code sample :)
    Last edited: Apr 2, 2009
  7. Apr 2, 2009 #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..
  8. Apr 2, 2009 #7
    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:


    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: Apr 2, 2009
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook