[VPM] Iterating through a dynamic array

nkuipers nkuipers at uvic.ca
Thu Jun 3 17:49:25 CDT 2004


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




More information about the Victoria-pm mailing list