But now we need the ability to vary our list of items --Davemy @items = (1,5,6,3,5,2,4); my $index = 0; sub do_next { do_something($items[$index]); $index = ($index+1) % @items; }
To understand the additional complexity in do_next, consider the case when: we hit the last item in the list; then add an item; then do_next() again.my @items = (1,5,6,3,5,2,4); my $index = -1; sub add_item { push @items, @_; } sub do_next { if (@items) { $index = ($index+1) % @items; do_something($items[$index]); } else { $index = -1; } }
Now the last part: deleting items while maintaining the index. The interesting facts are that the list may have duplicates; and if we delete an item that is earlier in the list than the current index, then we must decrement $index. Here's the code:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: round-robin on varying sequence
by BrowserUk (Patriarch) on Sep 07, 2002 at 07:03 UTC | |
by dpuu (Chaplain) on Sep 07, 2002 at 21:32 UTC | |
by BrowserUk (Patriarch) on Sep 08, 2002 at 00:12 UTC | |
by dpuu (Chaplain) on Sep 08, 2002 at 00:38 UTC | |
by BrowserUk (Patriarch) on Sep 08, 2002 at 00:58 UTC | |
|
Re: round-robin on varying sequence
by Arien (Pilgrim) on Sep 07, 2002 at 05:41 UTC | |
by dpuu (Chaplain) on Sep 07, 2002 at 19:12 UTC | |
|
Re: round-robin on varying sequence
by Zaxo (Archbishop) on Sep 06, 2002 at 23:35 UTC | |
by dpuu (Chaplain) on Sep 07, 2002 at 02:16 UTC | |
by Zaxo (Archbishop) on Sep 07, 2002 at 06:17 UTC | |
by BrowserUk (Patriarch) on Sep 07, 2002 at 08:02 UTC | |
|
Re: round-robin on varying sequence
by BrowserUk (Patriarch) on Sep 08, 2002 at 01:42 UTC |