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.


In reply to Re^5: foreach to for (related to my last question) by ikegami
in thread foreach to for (related to my last question) by tricolaire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.