in reply to RE: Re: Breaking a Loop
in thread Breaking a Loop

So you don't want to sort the array, you want to find the max value in it. So write a subroutine to find the max value in your array and call that (just like chromatic suggested!)
use strict; # ALWAYS!!!!!! # MaxElementInArray( @array ) sub MaxElementInArray { my $max = shift; my $next; $max = $next > $max ? $next : $max while $next = shift; return $max; } # MaxElementInArrayRef( \@array ) sub MaxElementInArrayRef { my $array_ref = shift; my $index = $#$array_ref; my $max = $$array_ref[$index]; $max = $$array_ref[$index] > $max ? $$array_ref[$index] : $max while --$index >= $[; return $max; }
Update: While examining your code in response to your statement below, I've noticed quite a few style issues: You don't use strict, you are not using CGI.pm, and you are not using subroutines to improve the readability (and maintainability) of your code. I strongly suggest you look into these things. As for your issue of trying to use a number before you have calculated it... this reveals a failure to plan before coding. Now I suspect you will need to re-write much of this code just to get things in a causal order.

Replies are listed 'Best First'.
RE: RE: RE: Re: Breaking a Loop
by Anonymous Monk on Oct 10, 2000 at 23:18 UTC
    Right, but there is still the problem that the array isnt defined until the end of the loop and that the sub routine has to be called within the loop. I need the max value of the final completed array, and returned into the loop..:-\.. when i wrote a subroutine it kept running it before it was called within the loop, and i would never get the final max value until the very end of the loop, by which point it was too late. thanks
    Dipul Patel
      You can't know something before you know it, unless you use Time::Machine.

      Time to rethink your algorithm. Say in english what you want to do. Avoid the word "loop" or "array". Just talk about what you want done, because you are now hopelessly down in the guts of the implementation with muddy thinking, and no-one can help you from there.

      -- Randal L. Schwartz, Perl hacker