in reply to Get biggest value from array

Ok, thanks. But how can I do this for only numerical values? Cause the array has also got mixed up, alphabetical records.

Regards,
Ralph.

Replies are listed 'Best First'.
Re: Re: Get biggest value from array
by hotshot (Prior) on Aug 25, 2002 at 07:13 UTC
    If your array conntains non numerical values then you just need to add a test for digits in theorbtwo's suggestion:
    foreach (@array) { if (/^\d+$/) { $max=$_ if ($_>$max) } }


    Hotshot
      Hi,

      If @array=(-3, -4, -2); then this code will fail.

      The $max variable should be initialised by shift()ing a value from @array.
      A small change would be required to the regex to allow negative numbers too. So we have
      #!/usr/bin/perl -w use strict; my @array=(-3,-4,-2); my $max=shift @array ; foreach (@array) { if (/^-?\d+$/) { $max=$_ if ($_>$max) } } print $max;
      cheers

      thinker