in reply to Getting values for next array iteration when matching current

Your code appears nonsensical and will not compile using "warnings" and "strict". Start over with a problem statement showing some input data and what you desire as output. Then show your code that will run using "warnings" and "strict" and its output and how that deviates from the desired output.

foreach (@ref)   #@ref is an undefined value, please describe at least in words what you think it means.