in reply to comparing array elements
How about, instead of using an array to hold the set of currently running widgets, use a hash. The hash (as I propose it) would have an entry for all of the widgets; the corresponding value would be a boolean indicating whether the thing is running or not. So instead of shuffling widget IDs into and out of a @running array, you just check/set/clear the hash values.
Here's the idea, neatly (though gratuitously) encapsulated:
{ package WidgetPool; my %widget_state; sub init { @widget_state{ @_ } = (0) x @_; } sub set_running { @widget_state{ @_ } = (1) x @_; } sub replace { my( $out, $in ) = @_; $widget_state{$out} = 0; $widget_state{$in} = 1; } sub is_running { my( $w ) = @_; $widget_state{$w} } } my @widgets = qw( foo bar baz blort blah plugh xyzzy arfle barfle gloo +p ); my @running = qw( foo bar baz ); WidgetPool::init( @widgets ); WidgetPool::set_running( @running ); # stand down 'foo', stand up $w: WidgetPool::is_running('foo') or croak "Um, 'foo' not running.\n"; WidgetPool::replace( 'foo', $w );
Update: fixed CnP bug in is_running. I never test posted code!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: comparing array elements
by mojodaddy (Pilgrim) on Mar 30, 2007 at 15:49 UTC | |
by pileofrogs (Priest) on Mar 30, 2007 at 16:50 UTC | |
|
Re^2: comparing array elements
by mojodaddy (Pilgrim) on Mar 30, 2007 at 17:08 UTC | |
by jdporter (Paladin) on Mar 30, 2007 at 19:50 UTC | |
by mojodaddy (Pilgrim) on Mar 30, 2007 at 20:25 UTC |