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

so the first version works, the second doesn't

the second version, when everything to do with $s++ and $s-- is removed, doesn't iterate enough times.

when the lines with $s are inserted, it goes through to many times and acts upon the wrong data sets. basically I'm asking: how do I replace the foreach loops with for loops?
  • Comment on Re^2: foreach to for (related to my last question)

Replies are listed 'Best First'.
Re^3: foreach to for (related to my last question)
by ikegami (Patriarch) on Jun 22, 2006 at 02:17 UTC

    basically I'm asking: how do I replace the foreach loops with for loops?

    And I asked why you want to do that. It's like asking how to remove weeds with kitchen utensils when you have perfectly good gardening tools. They're kinda similar, but they have different purposes.

    You have list over which you want to iterate. That's what "foreach" loops do. Counting ("for") loops, on the other hand, cannot iterate over lists. It's possible to store the list in an array, and loop over the indexes of the array, but why do you want to do that?

    To go from a "foreach" loop to a "for" loop, change

    foreach my $item (...list...) { ... }

    to

    my @list = (...list...); for my $i (0..$#list) { my $item = $list[$i]; ... }
Re^3: foreach to for (related to my last question)
by roboticus (Chancellor) on Jun 22, 2006 at 11:17 UTC
    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

      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