in reply to Re: Get biggest value from array
in thread Get biggest value from array

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

Replies are listed 'Best First'.
Re: Re: Re: Get biggest value from array
by thinker (Parson) on Aug 25, 2002 at 08:25 UTC
    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