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
    OK, I'm just in awe right now that you could do that in a fraction of the time it took me to write the question!

    After I'm finished weeping because of how unworthy I am, I'm going to delve into that. Thank you.

    Incidentally, I've noticed that when I get stuck on something, the way out seems invariably to involve hashes. Unfortunately right now the part of my brain that may someday be really comfortable with hashes is really just a kind of empty hole. It's like when I first hear "hash" I go into a trance of some kind.

    "Lost time" I think they call it...

      I eventually filled that particular hole in my head with 'if I ever need to search it, use a hash'. Now I use an even simpler method. 'Always use hashes unless there's a really good reason to use a list'.

      Okay, people who know better, please throw fruit at me...

Re^2: comparing array elements
by mojodaddy (Pilgrim) on Mar 30, 2007 at 17:08 UTC
    I keep wanting to think of "running" as "1" and "not running" as "0"; Which is causing me to have trouble with sub is_running, which, if I read it correctly, returns "0" if running. Whereas sub init sets an initial widget state of "0" and sub set_running sets them to "1"; However sub replace sets "out" to "0" ("out" to me indicating "out of the pool" i.e. running) and "in" (as in, back in the garage so to speak, or not running anymore) to "0";

    So if is_running returns "0" if foo is running, I would do something like:

    if (is_running('foo')){ # because "1" means "no" set_running('foo'); }
    which seems so unnatural that I'm sure I must be missing the point somewhere.

      No, that was a coding error on my part. I copied the line from the sub above, and forgot to delete the  =0 part. Sorry; my bad. (Fixed now.)

        Cool, so I'm not losing my mind!

        and since I actually got --'d for that, I guess I should ask: was there something about my question or how I asked it that was inappropriate or that was not in keeping with the spirit of PM? I certainly don't wish to offend anyone; my impression is that asking questions is OK. There are entire sections of the site devoted to it...

        Any guidance would be appreciated. It also might help curb whatever tendencies of mine are being frowned upon, thus minimizing the offensive behavior in the future.