in reply to List manipulation headache

Try this:

use List::Util qw[ reduce ]; $in = 'poe1 poe2 wap1 wap2';; $out = reduce{ $a .= $a=~/wap/ && $b=~/^wap(\d+)/ ? ",$1" : " $b" } split ' ', $in;; print $out;; poe1 poe2 wap1,2 $in = 'poe1 poe2 wap1 wap2 wap13 wap47';; $out = reduce{ $a .= $a=~/wap/ && $b=~/^wap(\d+)/ ? ",$1" : " $b" } split ' ', $in;; print $out;; poe1 poe2 wap1,2,13,47

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.