Hi predrag,
Welcome to Perl and PerlMonks :-)
Just a note on terminology: Those things in braces are typically called "blocks" (Update 2: not everything in braces is a block, though, for example there are anonymous hashes). "Loops" are normally for/foreach, while, and until, each of which have their own "blocks". So what you've got there are three if/elsif/else structures, with a total of four blocks. (Update: See Compound Statements)
There's nothing obviously wrong with the code you showed. Whether or not it's "correct" depends on whether your program works or not :-)
The best way to know whether your program works or not is tests. That is, a set of test input data and an expected output to which you can compare the program's actual output (there are ways to automate this, but that's a different topic). In your tests, it's useful to test as many branches of your code as possible, by setting up your test input data so that as many of the if/elsif/else blocks as possible are executed.
Perhaps we could give you some more comments if you could show a slightly longer, more representative sample - for example, something that includes the outer foreach loop and some short example data that it operates on (see also SSCCE). This is just a wild guess since I don't know anything at all about the structure of your input data and what you're doing with it, but perhaps one of these two variants of the logic might be more appropriate:
if ($char eq "a") { ... } elsif ($char eq "b") { ... } if ($k==2) { ... } else { ... } # OR if ($char eq "a") { ... if ($k==2) { ... } else { ... } } elsif ($char eq "b") { ... }
Regards,
-- Hauke D
In reply to Re: Begginer's question: If loops one after the other. Is that code correct? (updated)
by haukex
in thread Begginer's question: If loops one after the other. Is that code correct?
by predrag
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |