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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |