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 | |
by kcott (Archbishop) on Apr 25, 2014 at 07:10 UTC |