What you really want to work with here is a set instead of an array. Back in algorithms class we implemented a full set of operations for sets using arrays (in Pascal, no less!). I bet that would be easier using hashes. Or, there's Set::Scalar, Set::Formula, etc. for those averse to reinventing the wheel.