in reply to Re: Idiomatic Array Index Search?
in thread Idiomatic Array Index Search?
i agree that footpad has nothing to worry about here. however, i think the hash solution that makes sense isn't the one you argued against.
there's a hash solution that makes sense if three things are true:
what do i mean by "fairly" often and "fairly" frequently? i think probably if you are looking up 3 for each change you make to the array, and you can capture all array changes, then this is a good solution. as for #3, i'll assume you always want the 1st occurence.
the idea is, basically, to construct a lookup hash where for any $array[$i] == $value, there's $lookup{$value} == $i. for instance, you might write it this way:
.{ my %lookup; sub _init_lookup_hash { %lookup; for my $i (0..$#_) { $lookup{$_[$i]} = $i unless defined $lookup{$_[$i]}; } } sub list_index { return $lookup{$_[0]} or -1 } ; }
|
|---|