First off your listing these examples is a bit silly. In my previous node I recognized that C derivatives employ this horrible notation. Most likely to make the C programmers out there more comfortable. And AWK, LPC and Java are all C derivatives (ie they are member of the C side of the programming language family tree).

Second for my $i (map {$_ * 2} 0 .. 1000000 / 2) {$sum += $i} is nothing like the while statement. Nothing. At bare minimum the while statment does not iterate over a list twice.

But then, don't say that when we change the $i += 2 to $i ++ the alternative is a foreach. That would also be closer to the while.

On an abstract level the for(;;) while()continue() and foreach ($i..$j) are identical. At a implementation level the for(;;) and while() are identical, and for($i..$j) is not. And I think few people would say that the practical alternative to for(my $i;$i<10_000;$i++) is a while() structure. Certainly any programmers familiar with the for ($i..$j) would not.

I know the foreach is more efficient than the for, but only in the case of incrementing the variant by 1.

And that was precisely my point, and you know it.

The for() is a less error-prone, and easier way of writing the while.

I dispute this statement. I do not believe it to be true AT ALL, and even less true for NON-C (like language) programmers. And frankly until you can present some studies that show that cognitively people make less errors with the for(;;) construct than they do with while() im not even going to discuss it. And for two reasons: first I never made any statements regarding these two until this very paragraph, my point was the people make less errors in the case of iterating by 1 when using foreach. Second considering that a big chunk of the programmers out there have minimal exposure to it, I am seriously in doubt as to why a perfectly clear structure analagous or equivelent to structures available in just about every imperative language would produce more errors than one that is as cryptic as for(;;).

The only part of your last statement that I agree with is that the for(;;) form is easier to write than the while() form. And for good reason. Thats precisely why it was put in the language in the first place. A trade off of legibility for ease of use in application, especially when riduculously slow connections were the norm.

--- demerphq
my friends call me, usually because I'm late....


In reply to Re: Re: extracting corresponding values from another array by demerphq
in thread extracting corresponding values from another array 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.