in reply to Better way of doing!

This is a very crude way,I know. Please guide me with a better code.

Can you follow this? Re: Efficient matching with accompanying data (tree)

See also Efficient matching with accompanying data, perlintro#Regular expressions / Simple matching %uniq/%seen/%count