in reply to Re^2: foreach to for (related to my last question)
in thread foreach to for (related to my last question)

tricolaire:

Just to amplify on the point ikegami makes: foreach takes care of the niggling little details for you:

  • 0-based or 1-based array? (I code in several languages, so it doesn't stay burned in.)
  • Is my condition == or >=?
  • How do I handle the case if my code adding an item to the list inside the code?
  • Having a nice iterator handle the simple details is a blessing. Why not use it?

    --roboticus

    Replies are listed 'Best First'.
    Re^4: foreach to for (related to my last question)
    by tricolaire (Novice) on Jun 22, 2006 at 13:16 UTC
      because I'm trying to change the language in which the algorithm is written. in c++ I don't have the use of the foreach loop, at least, the for_each loop I do have doesn't allow me to change the list.

      so I can't figure out how to deal with the stuff I add or remove, so any help would be nice

        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.

        tricolaire:

        OK, then, when you translate it to C++, make your life simpler and use STL. Then you'll find that your hashes, vectors and foreach loops translate fairly easily:

        vector<X> aas; vector<X>::iterator aa; map<K,V> SEQ; map<K,V>::iterator seq; vector<Z> codons; vector<Z>::iterator codon; for (aa=aas.begin(); aa!=aas.end(); ++aa) { for (seq=SEQ.begin(); seq!=SEQ.end(); ++seq) { for (codon=codons.begin(); codon!=codons.end(); ++codon) { ... do something ... } } }
        Also, what I've shown isn't great STL. There are functors and predicates that let you do things like map and grep. Give it a try. You'll find that it makes C++ much nicer. (Though it does take a bit of time to wrap your head around it when you start.)

        --roboticus

          Are you sure it's safe to add and delete items from a map while iterating over it? I personally don't know, so I'm just raising the issue.