As a side note: Python has no labels, and loop control statements like break are restricted to the inner loop.

So this "natural" solution in Perl (that is w/o flag variable) isn't possible in Python

N: for my $n (2 .. 99) { for my $x (2 .. $n - 1) { next N if 0 == $n % $x; } say $n, ' is a prime number'; }

Looking at a one to one translation of the flow, it bugs me that the "NOT_PRIME" part is executed after the "PRIME" part, which is quite strange in my eyes

for my $n (2 .. 99) { for my $x (2 .. $n - 1) { goto NOT_PRIME if 0 == $n % $x; } PRIME: say $n, ' is a prime number'; NOT_PRIME: }

So to separate code which doesn't belong to the py-else branch one needs to put it into the loop into the py-if branch

so the logic of this Perl code with flag-var prime

for my $n (2 .. 99) { my $prime = 1; for (2 .. $n-1) { $prime = 0, last unless $n % $_; } if ($prime) { print $n, " is "; } else { print $n, " is not "; } print "a prime number\n"; }

translates to

for my $n (2 .. 99) { for my $x (2 .. $n - 1) { if (0 == $n % $x) { # py-if branch print $n, " is not "; goto BREAK; # py-break } } print $n, " is "; # py-else branch BREAK: print "a prime number\n"; }

I think now the motivation to call this statement "else" it's better understandable. But the py-docs do not seem to provide any such motivation.

Maybe it's a matter of practice, but I rather prefer the ability to have Perl labels and loop controls like next or last to address them.

And Perl's goto still gives me the freedom to emulate any python code in a 1-to-1 translation. And the naming of the label in Perl gives me additional freedom to clarify the code! ¹

Python OTOH can't emulate labels or Perl's control flow and needs to redesign the code when migrated.

See also this SO- discussion http://stackoverflow.com/questions/438844/is-there-a-label-goto-in-python for how quirky and complicated py-workarounds can become...

Cheers Rolf

( addicted to the Perl Programming Language)

¹) Please note that I named the "BREAK:"-label as "NOT_PRIME:" in my first post, thats very self-commenting and easier to understand than simply "else:"


In reply to Re^2: Perl vs. Python (for/else idiom analyzed) by LanX
in thread Perl vs. Python for prime numbers by Anonymous Monk

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.