Why does my Perl max() function always return the first element of the array?

arrays, foreach, max, perl

Solution

Replace

my @array = shift;

with

my @array = @_;

`@_` is the array containing all function arguments. `shift` only grabs the first function argument and removes it from @_. Change that code and it should work correctly!

Problem

I am relatively new to Perl and I do not want to use the List::Util `max` function to find the maximum value of a given array. When I test the code below, it just returns the first value of the array, not the maximum. ``` sub max { my @array = shift; my $cur = $array[0]; foreach $i (@array) { if($i > $cur) { $cur = $i; } else { $cur = $cur; } } return $cur; } ```

Original source