Well, it seems that in the meanwhile you have solved your problem. My point was about preparing a suitable
complete but
minimal example still exhibiting the problem. This is a general recommendation as to how to ask for help, and sometimes it's hard or even impossible to do, but that's rare. More commonly it even happens that while doing so one will find the answer to his own question.
Now, as side notes to comment you code:
- I wouldn't use all those nested if's; I'd recommend you to read up something about next, last and while you're there also redo,
- I wouldn't assign to $dummy[++$#dummy], but stick with push, since it's what it's actually for.