It it's only about keeping pairs together, loop over pair names separated by some delimiter character, like "STOCK1|STOCK2", then parse the ticker names out of the pair name.