Hi vr,

You say: Next sub prevents these shortcomings, but uses Perl's "no-no" -- the C-style loop.

Your "sort-of-C-looking-code" here, is close to a no-no in C:
    for ( my $i = 0; $i < @$aref; $i ++ )

In C, the real "work horse" is the while loop and pointers, incrementing pointers and pointers to arrays of pointers to pointers and dereferencing these pointers to test for NULL or other values to stop the loop.

The array syntax x= i[3] is used as a "last resort" in good C code. There are definitely reasons use the index syntax, but this is not preferred. One issue is that accessing the memory address of i[3] is often caculated by the complier as 4 * size_of_an_i_element + base address of array i whereas incrementing a memory pointer through an array is just: pointer + size_of_an_i_element. Less math == faster.

Back to Perl...
The number one "bug" in sofware is the "off-by-one" error. The Perl code of foreach my $ref (@$aref){} would save you from that.
An issue in the C-like loop: for ( my $i = 0; $i < @$aref; $i ++ ) comes with the $i < @$aref test and how that relates to the initial value and the use of i as an array index.

A much bigger issue with your code appears to be the equating of "fewer source code lines" with "better" or "higher performance". That is definitely NOT true. In many cases shorter Perl can run much slower than more verbose implementations. I think you misused the Perl "map" function. "map" is a cute, short hand way to write a foreach loop. Normally map is used for one line "transformation" operations. For more than that, I use a foreach loop. There is no difference in the performance, but a big difference in the clarity of the code.

At the end of the day, I agree the other Monks who have posted as to the confusing nature of OP code. I disagree with your idea of how to implement this in "C like Perl". I don't understand things well enough to suggest an alternative implementation.


In reply to Re: To splice or not to splice? What implementation is more readable, efficient, etc.? by Marshall
in thread To splice or not to splice? What implementation is more readable, efficient, etc.? by vr

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.