home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 92 / af092a.adf / af92.lzx / ARexx_Code / bubble_sort.rexx next >
OS/2 REXX Batch file  |  1996-10-30  |  2KB  |  103 lines

  1. /* ---------------------------------------------- */
  2.  
  3. /* bubble_sort.rexx - sorts the words in a text file into order */
  4.  
  5. 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.         /* now we bubble sort... */
  51.             
  52.         do i=total_count to 1 by -1
  53.        
  54.            do j=2 to i
  55.                
  56.               x=j-1
  57.                
  58.              if Upper(word.x)>Upper(word.j) then
  59.                
  60.                 do
  61.                   
  62.                   temp=word.x
  63.                   
  64.                   word.x=word.j
  65.                   
  66.                   word.j=temp
  67.                   
  68.                 end
  69.                
  70.             end
  71.        
  72.          end
  73.            
  74.       
  75.        /* now write data and close files before exiting... */
  76.        
  77.      do i=1 to total_count
  78.      
  79.        call Writeln(d,word.i)
  80.      
  81.      end
  82.  
  83.      Close(d)
  84.           
  85.      say 'Function complete!'
  86.       
  87.      end
  88.  
  89.   else say 'Sorry - cannot open destination file!'     
  90.    
  91.   Close(s)
  92.       
  93.   end
  94.  
  95. else say 'Sorry - cannot open source file!'
  96.  
  97. exit
  98.  
  99. /* ---------------------------------------------- */
  100.  
  101.  
  102.  
  103.