http://qs321.pair.com?node_id=1083512


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

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 = ...; } }

Replies are listed 'Best First'.
Re^4: Iterating through an array using multiple loops and removing array elements
by BiochemPhD (Novice) on Apr 24, 2014 at 05:11 UTC
    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 ) = @_;
        You understood correctly. My mistake was in not realizing that the subroutine arguments (correct me if I'm wrong) would automatically be $_[0] ($top_entry) and @keepers (@entries).

        Thanks for your help so far. I feel like I may be a few steps away from accomplishing my scripting goal.