Try this. It does it in 1 pass and creates only 1 temporary list as opposed to 2 half passes, 3 temporary lists (5 if you count the ranges inside the slices), and 2 temporary arrays, so I think it should work out to be faster.

I realise that I am omitting the lists used to create the hashes, but we both use one, so that balances out.

I've been trying to benchmark the two variations, but for reasons I haven't yet fathomed, I have yet to succeed. I'll update if I do.

sub remove_item { my ($i, %del) = (0); @del{@_} = undef; @items = grep { $i++ and !exists $del{$_} and $index -= ($i < $ind +ex) } @items; }

Update:I got the benchmarking to go.

#! perl -sw use strict; use Benchmark; my @i = qw( a b c x d e f a g h i j k l z m n o p q r t s t b u v w x +y z ); my @dups = qw( x a z t b ); my $index = 13; my @items = @i; sub remove_item { my %del = map { $_ => 1 } @_; my @part_1 = grep { !exists $del{$_} } @items[0..$index]; my @part_2 = grep { !exists $del{$_} } @items[$index+1..$#items]; $index = $#part_1; @items = (@part_1, @part_2); } sub remove_items { my ($i, %del) = (0); @del{@_} = undef; @items = grep { $i++ and !exists $del{$_} and $index -= ($i < $ind +ex) } @items; } print "@dups\n"; @items = @i; $index = 13; print "$index: $items[$index] : @items\n"; remove_item @dups; print "$index: $items[$index] : @items\n"; print $/ x 2; print "@dups\n"; @items = @i; $index=13; print "$index: $items[$index] : @items\n"; remove_item @dups; print "$index: $items[$index] : @items\n"; print $/ x 2; Benchmark::cmpthese( 1000, { mine => sub { @items = @i; $index=13; remove_items @dups; }, yours => sub { @items = @i; $index=13; remove_item @dups; }, }); __DATA__ # Output C:\test>195796 x a z t b 13: l : a b c x d e f a g h i j k l z m n o p q r t s t b u v w x y z 9: l : c d e f g h i j k l m n o p q r s u v w y x a z t b 13: l : a b c x d e f a g h i j k l z m n o p q r t s t b u v w x y z 9: l : c d e f g h i j k l m n o p q r s u v w y Benchmark: timing 1000 iterations of mine, yours... mine: 0 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 CPU) @ 19 +60.78/s (n=1000) yours: 1 wallclock secs ( 0.64 usr + 0.00 sys = 0.64 CPU) @ 15 +62.50/s (n=1000) Rate yours mine yours 1562/s -- -20% mine 1961/s 25% -- C:\test>

I agree that this isn't the way I would tackle the overall problem, but I enjoyed playing with it!


Well It's better than the Abottoire, but Yorkshire!

In reply to Re: round-robin on varying sequence by BrowserUk
in thread round-robin on varying sequence by dpuu

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.