Re: converting sequence to string
- Posted by Nick Johnson <arachnid at MAD.SCIENTIST.COM> Mar 26, 2000
- 644 views
©koda wrote: > does anybody have the function that converts sequences with many other > sequences under it, and sequences under it, into one sequence which > has just atoms(=string)i have tried to write such function but it > would be quite complicated because there can be unlimited number of > sequences under sequences... Yes! This is just screaming to be an application of one of my favourite topics (more often known as programmers bane), Recursion!!! Yes, a routine that calls itself (a simple one unfortunately). It's amazinh how often a complex looking problem resolves itself when you try recursion. I can see how this routine would be useful, especially (and this applies to everyone) when you want to print a sequence with nested strings (although you lose the nesting). That reminds me - would that be any use to anyone? a puts that puts nested strings with the braces around them to indicate where each one is? So something like this: {Hello{How{Are you}?}This is an{example of}nested strings}, or perhaps with commas before and after the braces. Anyway, here's the code: global function deNest(sequence nestedSequence) --deNests a sequence by recursing through it and 'flattening' it to 1 level sequence outputSequence outputSequence = {} for a=1 to length(nestedSequence) do if not sequence(nestedSequence[a]) then outputSequence = outputSequence & nestedSequence[a] else outputSequence = outputSequence & deNest(nestedSequence[a]) end if end for return outputSequence end function Enjoy! Nick