You haven't really explained what you're ultimately trying to achieve here.
If you put your question in terms of a concrete context, you'll probably get a better answer. Guidelines for doing this can be found here: How do I post a question effectively?

The following piece of code may do what you want or at least give you some idea of how to proceed.