Walking Trees Backwards

Jim Ault JimAultWins at yahoo.com
Fri Sep 12 00:29:01 EDT 2008


To make tab/space runs, this is the function I use

function getIndentSpaces pLevel
  put space into line (pLevel*4) of tempp
  replace cr with space in tempp
  return tempp
end getIndentSpaces

put 1 into tDepth
put "Al, child of the first marriage" into childString
put getIndentSpaces(tDepth) & childString & cr after listing
add 1 to tDepth
put "Betty, child of Al" into childString
put getIndentSpaces(tDepth) & childString & cr after listing
put listing into msg

-----------------
--the same technique will work with
put space into item (pLevel*4) of tempp
replace comma with space in tempp

Of course you could use TAB instead of space.

Jim Ault
Las Vegas

On 9/11/08 6:21 PM, "Alex Tweedly" <alex at tweedly.net> wrote:

> 
>> function buildTree pInput
>>    # given indented outline of a 'forest' (i.e. multiple rooted trees)
>> build the tree-like data structure
>>    set the itemDel to TAB
>>    put empty into lParents
>>    put 0 into lCurNode # the top-level pseudo-node
>>    put -1 into lCurDepth  # -1 to allow for the top-level pseudo-node
>>    put 0 into lCount
>>    repeat for each line L in pInput
>> 
>>       put getDepth(L) into tDepth
>>       if tDepth < lCurDepth then
>>          # we have popped back up 1 or more levels
>>          put tDepth into lCurDepth
>>          if tDepth = 0 then
>>             put 0 into lCurNode
>>          else
>>             #put item tDepth+2 of lParents into lCurNode
>>             put lCount into lCurNode
>> 
>>          end if
>>       else
>>          if tDepth = lCurDepth then
>>             # another node at the same level - no change needed
>>             put lCount into lCurNode
>>          else
>>             if tDepth > lCurDepth+1 then # badly formed input  !!
>>                # should probably do something more extreme here
>>                return empty
>>             end if
>>             # so we are here with a new increase in indent level
>>             put lCurNode & TAB after lParents
>> 
>>          end if
>>       end if
>>       put L into Data[lCount, 'text']
>>       put item -1 of lParents into tParent
>>       put tParent into Data[lCount, 'parent']
>>       put lCount & TAB after Data[tParent, 'children']
>>       put tDepth into lCurDepth
>>    end repeat
>>    return Data
>> end buildTree





More information about the use-livecode mailing list