[VPM] Iterating through a dynamic array

Jeremy Stashewsky jhs at uvic.ca
Thu Jun 3 18:19:34 CDT 2004


How about using a "hash set" to keep track of which elements to delete?

my %to_keep = map {$_=>1} (0..$#score_matrix);
foreach my $i (0..$#score_matrix) {
    if ($score_matrix[$i] > $LIMIT) {
        delete $to_keep{$i};
    }
}
my @keepers = sort {$a <=> $b} keys %to_keep
@score_matrix = @score_matrix[@keepers];
@init = @init[@keepers];  # is this your other array?

Likewise, you could add in the elements you wanted to keep into the hash
instead of deleting the ones you don't want.
I *guess* you could also use a list of indices you wanted to keep, splicing
out the elements you don't want as you go along -- this approach will save
you the sort() call near the end.
Which strategy is faster will probably depend on which operation is faster:
deleting from a hash or splicing out from a list.

Disclaimer: I didn't actually test any of this, but it should work.

~jeremy

----- Original Message ----- 
From: "nkuipers" <nkuipers at uvic.ca>
To: <victoria-pm at pm.org>
Sent: Thursday, June 03, 2004 3:49 PM
Subject: [VPM] Iterating through a dynamic array


> Hello all,
>
> I am rather stuck on this, and would like some help please.
>
> I have 2 arrays, the first contains a bunch of string refs, the second a
bunch
> of numeric scores, where the indeces of the second array correspond to
those
> of the first.  So, the stringref in array_one[0] has a score contained in
> array_two[0].  Now, I want to iterate through the scores, and if a score
> doesn't meet a certain threshold value, I want to splice() out the
> corresponding stringref.  Well, that's fine for the first splice
operation,
> but then I am stuck with two sets of indeces that no longer correspond.  I
> suppose I could splice out of both arrays to keep them the same size, and
then
> restart the iteration, but then I am redoing score comparisons that
already
> passed in order to get to the next splice candidate, and that's ick.  For
> those of you you speak better in code than prose, here it is, slightly
> beatified:
>
> my @init; # gets populated with stringrefs
> ...
>
> remove_similar(get_similarity_matrix($shifted_stringref_from_init));
>
> # similarity() is from String::Similar on CPAN
> sub get_similarity_matrix {
> my ($seq1) = @_;
> my @score_matrix = ();
> foreach my $seq2 (@init) {
> push @score_matrix, similarity($$seq1, $$seq2);
> }
> return \@score_matrix;
> }
>
> sub remove_similar {
> my @score_matrix = @{ shift @_ };
> for (my $i = 0; $i <= $#score_matrix; $i++) {
> if ($score_matrix[$i] > $LIMIT) {
> splice @init, $i, 1; # ACK!!
> }
> }
> }
>
> Thanks for any insight,
>
> Nathanael Kuipers, BSc. (CD)
> -----
> Center for Biomedical Research
> University of Victoria
> email: nkuipers at uvic.ca
>
> _______________________________________________
> Victoria-pm mailing list
> Victoria-pm at pm.org
> http://www.pm.org/mailman/listinfo/victoria-pm
>




More information about the Victoria-pm mailing list