Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Iterating through an array using multiple loops and removing array elements

by frozenwithjoy (Priest)
on Apr 24, 2014 at 04:39 UTC ( #1083512=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1083512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2021-01-27 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?