Huh?
Do you have pseudo code or something for us to work with?
I can answer "yes, that there is a way", but that wouldn't help you in solving your problem anymore than your description helped me in understanding your problem.
Post again and use variable names (even temporary ones, as long as they are descriptive) and tell us what you imagine a variable should hold in your script.