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

G'day BiochemPhD,

Welcome to the monastery.

Firstly, modifying an array while you're looping through it (with for[each]) will cause problems. The documentation is quite clear on this. From "perlsyn - Foreach Loops":

"If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that."

From your various posts in this thread, I think this is fairly close to what you want:

#!/usr/bin/env perl -l use strict; use warnings; use List::Util qw{first}; my $min = 3; my @records = reverse(0 .. 10, 13, 17, 42); print "All records: @records"; my %deleted; for my $i (0 .. $#records) { my $top_index = first { ! $deleted{$_} } $i .. $#records; last unless defined $top_index; my $top = $records[$top_index]; my @group = ($top); for ($top_index + 1 .. $#records) { next if $deleted{$_}; if (compare_sub($top, $records[$_]) <= $min) { push @group, $records[$_]; ++$deleted{$_}; } else { last; } } ++$deleted{$top_index}; print "Group: @group"; } sub compare_sub { my ($x, $y) = @_; return abs($x - $y); }

Output:

All records: 42 17 13 10 9 8 7 6 5 4 3 2 1 0 Group: 42 Group: 17 Group: 13 10 Group: 9 8 7 6 Group: 5 4 3 2 Group: 1 0

Obviously, I've had to dummy up input data and the compare_sub() routine; however, this does seem to match your (rather vague) description of "abundance".

As this solution doesn't actually modify @records at all, you may find some benefit in using the builtin module Tie::File: it doesn't load the file into memory (so that may be useful depending on the record size of your thousands of records) and it's less coding (than what I can only guess you're currently doing).

I appreciate this is your first post (and, to be honest, it's a lot better than many first posts). Please just note the various difficulties monks had and keep those in mind whenever you post next.

-- Ken

Replies are listed 'Best First'.
Re^2: Iterating through an array using multiple loops and removing array elements
by BiochemPhD (Novice) on Apr 24, 2014 at 15:59 UTC

    Modifying an array while looping with foreach = not good.

    What about while looping though it with while? Similarly, what about modifying a hash (using delete) while looping with for/foreach or while? From a quick skim through the doc it doesn't appear to be an issue.

    I'll definitely keep in mind the shortcomings of this post in the future! Context is especially important when TIMTOWTDI!

    Thanks for the help!

      "What about while looping though it with while?"

      for (@array) iterates over the list of values in @array. Changing that list in mid-iteration often has problems. We actually get quite a few questions like "Why doesn't my for loop work?" that are due to such a problem. So, as the doco says, "don't do that".

      while (@array) involves no iteration; it's a simple condition which basically says "Enter the loop if @array has any elements". Unless there's some other method for exiting that loop, you would expect elements to be removed from @array so that the loop can eventually terminate.

      "Similarly, what about modifying a hash (using delete) while looping with for/foreach or while? From a quick skim through the doc it doesn't appear to be an issue."

      If you're writing for (%hash) or while (%hash), that's probably a mistake; perhaps you meant something else. You'll need to provide some code to show the scenario(s) you're considering here.

      By the way, in case you didn't know, for and foreach are synonymous. Save yourself four keystrokes by writing for instead of foreach: the code will run the same whichever you choose.

      -- Ken