home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 92 / af092a.adf / af92.lzx / ARexx_Code / file_tree_sort.rexx < prev    next >
OS/2 REXX Batch file  |  1984-02-16  |  2KB  |  129 lines

  1. /* ---------------------------------------------- */
  2.  
  3. /* file_sort.rexx - sorts the words in a text file into order */
  4.  
  5. ROOT=1; tree.=0; word.=''; total_count=0
  6.  
  7.  
  8. /* ---------------------------------------------- */
  9.  
  10. /* first step is to read all lines from the file and 
  11.    place words into the word. compound variable set... */
  12.  
  13.  
  14. say "enter source filename and path?"; pull source_name
  15.  
  16. say "enter destination filename and path?"; pull dest_name
  17.  
  18. if Open(s,source_name,'r') then 
  19.  
  20.    do 
  21.  
  22.      if Open(d,dest_name,'w') then 
  23.  
  24.       do 
  25.       
  26.         do while ~EOF(s)
  27.       
  28.           text=Readln(s)
  29.            
  30.           count=Words(text)
  31.           
  32.           if count>0 then 
  33.           
  34.             do   
  35.                        
  36.               do i=1 to count
  37.                  
  38.                  j=total_count+i
  39.  
  40.                  word.j=Word(text,i)
  41.                
  42.               end
  43.              
  44.               total_count=total_count+count
  45.        
  46.             end
  47.            
  48.          end    
  49.      
  50.        /* then we just do the same tree operations as before... */
  51.  
  52.        tree.ROOT
  53.  
  54.        do i=2 to total_count
  55.    
  56.           call BuildTree(i,ROOT)
  57.         
  58.        end
  59.        
  60.        call PrintTree(ROOT)
  61.  
  62.        /* and then close files before exiting... */
  63.        
  64.        Close(d)
  65.      
  66.      say 'Function complete!'
  67.       
  68.      end
  69.  
  70.   else say 'Sorry - cannot open destination file!'     
  71.    
  72.   Close(s)
  73.       
  74.   end
  75.  
  76. else say 'Sorry - cannot open source file!'
  77.  
  78. exit
  79.  
  80. /* ---------------------------------------------- */
  81.  
  82. BuildTree: Procedure expose tree. word.
  83.  
  84. parse arg i, node
  85.  
  86. if Upper(word.i)<=Upper(word.node) then
  87.  
  88.          do
  89.    
  90.            if tree.node.left=0 then tree.node.left=i
  91.    
  92.            else call BuildTree(i,tree.node.left)
  93.    
  94.          end
  95.    
  96.   else   do
  97.    
  98.            if tree.node.right=0 then tree.node.right=i
  99.    
  100.            else call BuildTree(i,tree.node.right)
  101.    
  102.          end   
  103.  
  104. return    
  105.  
  106.  
  107. /* ---------------------------------------------- */
  108.  
  109. PrintTree: Procedure expose d tree. word.
  110.  
  111. parse arg node
  112.  
  113. if node ~=0 then
  114.  
  115.         do
  116.         
  117.           call PrintTree(tree.node.left)
  118.           
  119.           call Writeln(d,word.node)
  120.          
  121.           call PrintTree(tree.node.right)
  122.         
  123.         end
  124.        
  125. return
  126.         
  127. /* ---------------------------------------------- */
  128.  
  129.