in reply to map2 {} grep2 {} ...

Consider "each." Also consider a while loop that shifts the first variable off an array in the "while" part, then shifts the remainders off during the body of the loop (using "last unless" as needed).