tag:blogger.com,1999:blog-32079676.post479490406992446739..comments2024-02-13T21:22:02.522-08:00Comments on RRResearch: Who knew that 'while' loops can't be nested...Rosie Redfieldhttp://www.blogger.com/profile/06807912674127645263noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-32079676.post-54705879893198431892008-05-01T11:54:00.000-07:002008-05-01T11:54:00.000-07:00Yes, Neil's right; nested while loops are allowed ...Yes, Neil's right; nested while loops are allowed in Perl, but typos, pastos, or thinkos can break any code.<BR/><BR/>Short (valid) sample:<BR/>---<BR/>#!/usr/bin/perl -w<BR/>use strict;<BR/><BR/>my $a=0;<BR/><BR/>while ($a<15) {<BR/> $a++;<BR/> print "Outer a: $a\n";<BR/> while ($a<10) { <BR/> $a++;<BR/> print "Inner a: $a\n";<BR/> }<BR/>}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-32079676.post-43295845868547730492008-04-22T18:32:00.000-07:002008-04-22T18:32:00.000-07:00It's hard to diagnose the code without seeing it b...It's hard to diagnose the code without seeing it but in principle, nothing prevents nested while() loops. A couple of common "gotchas":<BR/><BR/>1. Infinite loops. This often happens when using while() to find a regex and forgetting the 'g' switch. For example to find all occurences of 'ATCG' in string $seq:<BR/><BR/>while($seq =~/ATCG/g)<BR/><BR/>This would run forever if you forgot the 'g'.<BR/><BR/>2. As soon as one while() test is no longer true, the loop will exit to the next (one higher) level of the nest. If the first while() test in the nest is not true, none of the inner while() loops will run. In other words, it's good to know what you expect from each while() test.<BR/><BR/><BR/>On the curly brackets problem: it really helps if you use an editor that highlights and validates Perl (or any other language). For example there's an emacs perl mode: if you position the cursor next to a bracket, it will highlight that bracket and its partner. I'm sure there are many Mac editors with the same function.<BR/><BR/>When writing loops it's good to type all of the opening + closing brackets when you start, before the loop contents. Also align matching opening/closing pairs so as they are below each other in the same column - makes it easier to see which belong with which.Neilhttps://www.blogger.com/profile/14642902803329530119noreply@blogger.com