in reply to Advice on loops

What do you want to happen when @arrayindex has only one element? Given your original hardcoded code, there will be no enteries in the hash.