You wouldn't use a "for" loop in C++ either. Your C++ equivalent to a hash should have an iterator, and that would be used in conjunction with a "while" loop.
Update:
But you have a point about not changing the list. You can't in Perl either. Try modifying a hash over which while each is interating. foreach keys, on the other hand, makes an anonymous copy of the hash keys before the loop begins, so you are not modifying the list over which you are iterating. You'll have to do the same (create a copy) in C++. However, the copy will have to be named (not anonymous) in C++.
foreach my $seq (keys %SEQ) {
foreach my $codon (@codons) {
$SEQ{$seq.$codon} = 1;
}
delete $SEQ{$seq};
}
would look more like:
my @seq_keys = keys %SEQ;
foreach my $seq_key_num (0..$#SEQ) {
my $seq = $seq_keys[$seq_key_num];
foreach my $codon (@codons) {
$SEQ{$seq.$codon} = 1;
}
delete $SEQ{$seq};
}
@seq_keys is the copy in the above snippet. Notice how we are iterating over (the indexes of) @seq_keys and not over (the keys of) %SEQ? foreach does the same thing, but you just don't see foreach doing it. That's why it's safe to modify %SEQ.
Your question deceptivly looks like a Perl question, but it has nothing to do with Perl. I had to convert from C++ to Perl (in my head) to answer you. Having a discussion in Perl on the subject of C++ doesn't make it a discussion about Perl any more than having a discussion in French on the subject of English language makes it a discussion about French. Please continue this discussion in a more appropriate forum.
|