ARexx Sort
6 messages in this thread
I would like to sort a list in ARexx, I thought there was a fn for this
already, but can't find it. I am about to send the list to a temp. file,
then use dos sort on it.. Anybody have a more elegant solution? (no I don't
care to write a sort that will be longer than the program)
That is probably the easiest way, I wrote a bubble sort in AREXX once, but
found that using AmigaDOS sort is much faster and easier. If all you need
is to find the largest number or smallest number look at MAX() and MIN().
Greg
Greg Givler – CBMSER Sysop
Commodore Product Assurance
I'm processing a list of filenames… I think the results would look better
sorted.. I guess its dos sort then! thanks
Arnie,
Here's a rexx shellsort routine from the latest book THE AREXX COOKBOOK
by Merrill Callaway. I highly recommend the book. Hope this routine
helps.
/* shellsort.rexx input the file for sorting */
PARSE UPPER ARG infile' 'outfile
IF infile = '' THEN DO
SAY 'Input sort filename and path: '
PARSE PULL infile
END
RC=OPEN('sortfile',infile,'READ')
IF ~RC THEN DO
SAY 'File not opened. Separate arguments with space no comma.'
EXIT
END
k=1
DO WHILE ~EOF('sortfile')
list.k=READLN('sortfile')
k=k+1
END
listlength = k-2
SAY listlength 'entries to sort…'
/* The Shell Sort */
RC=TIME('R')
span = 1
DO WHILE (span < listlength); span = span * 2; END
DO WHILE (span > 1)
span = span % 2
numpairs = listlength – span
DO node = 1 TO numpairs
nextnode = node + span
IF list.node > list.nextnode THEN
DO
store = list.nextnode
list.nextnode = list.node
DO bubpos = node-span TO 1 BY -span WHILE (store < list.bubpos)
nextnode = bubpos + span
list.nextnode = list.bubpos
END bubpos
bubpos = bubpos + span
list.bubpos = store
END
END node
END
SAY 'Elapsed time='TIME('E')' seconds.'
/* output results */
IF outfile = '' THEN DO
SAY 'Specify filename and path for sorted output.'
PARSE PULL outfile
IF outfile='' THEN DO
SAY 'No output written.'
EXIT 5
END
END
CALL OPEN('outfile',outfile,'WRITE')
DO i=1 TO listlength
CALL WRITELN('outfile',list.i)
END i
SAY 'Output has been written to 'outfile
EXIT 0
Thanks! I just got that book, and haven't had a chance to go through it
yet. I also got a new ARexx version with a sort server included… so many
options…
The sort server is very good. It uses a "quicksort" algorithm and since
it's compiled will be quicker than anything in AREXX itself. — Art