Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Stepping through an array and making changes "as-you-go" has unforeseen problems. We get quite a few "Why didn't this work?" questions related directly to attempting to do this type of thing. The issue is actually documented in "perlsyn: Foreach Loops" and specifically mentions splice: "If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that." [In case you didn't know, for and foreach are synonymous.] So, you'd need to do something like the following.
I plugged that in to my earlier benchmarking script. Here's a representative result:
My coding of splice_unique2() was really just intended to show a way to get around the documented issue. If you think the task has any merit, feel free to try to rewrite it to run faster. -- Ken In reply to Re^6: Create unique array --the hard way!
by kcott
|
|