for ($ref=0; $ref<$#items; $ref++) { for (@{$items[$ref]}) { if (!$contains{$_}) { $contains{$_} = [$ref]; } else { push @{$contains{$_}}, $ref; } } }