in reply to Re: Iterating through an array using multiple loops and removing array elements
in thread Iterating through an array using multiple loops and removing array elements

I've thought about that but the problem is that after I take the zeroth element and compare it to everything else in the array (removing both the zeroth element and any that meet the comparison criteria), I want to then move onto the next remaning element and repeat the process over and over again until the entire list has been consumed.
  • Comment on Re^2: Iterating through an array using multiple loops and removing array elements

Replies are listed 'Best First'.
Re^3: Iterating through an array using multiple loops and removing array elements
by frozenwithjoy (Priest) on Apr 24, 2014 at 04:39 UTC

    What about making a subroutine for the iterative comparison and have it recursively call itself on the kept values from the array unless some condition is true?

    iterate(@entries); sub iterate { my ( $top_entry, @entries ) = @_; my @keepers; for (@entries) { my $comparison = compare_sub( $top_entry, $_ ); if ( $comparison > $user_defined_value ) { push @keepers, $_; } } iterate(@keepers) unless ...; }

    EDIT: A variation where you continue iterating or assign final result depending on some condition. It's hard to know the right approach from here w/o more info

    my @final_result; iterate(@entries); sub iterate { my ( $top_entry, @entries ) = @_; my @keepers; for (@entries) { my $comparison = compare_sub( $top_entry, $_ ); if ( $comparison > $user_defined_value ) { push @keepers, $_; } } if (...) { iterate(@keepers); } else { @final_result = ...; } }
      Recursive iteration through a subroutine might work, but I can't figure out how I would reset the value for $top_entry after one iteration through.
        Unless I misunderstood, didn't you say the next top entry is derived from the first element in your new list of kept elements? If this is the case, the following line takes care of the problem. If this is not the case, then it would be helpful to have a more explicit set of specs for the script.
        my ( $top_entry, @entries ) = @_;