Marked as: Easy
Hello,
Here's a couple of simple scripts to sort rows in text files.
The rexxutil function SysStemSort, almost not documented, provide a very convenient way to handle it as you can see below.
I use the first script to sort e.g. the handmade file (drag & drop individual files on script) that specify what files to burn to the DVD with DVDDAO (http://"http://hobbes.nmsu.edu/h-search.php?key=dvddao&pushbutton=Search")
This code use a dummy character '¤' to distinguish the file name from the rest of the text, thus making the file name the primary sorting factor.
One would probably wish that it rather save the sorted rows in another file, but both examples append the sorted rows to the original file./* ROWSORT on FILENAME */
call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
call SysLoadFuncs
row = 1
do while lines( ARG(1) ) > 0
cfg.row = linein( ARG(1) )
parse value cfg.row with pre'='post
if length( pre ) > 1 then
cfg.row = filespec( 'N', pre )'¤'cfg.row
else
cfg.row = filespec( 'N', post )'¤'cfg.row
row = row + 1
end
call stream ARG(1), 'C' 'CLOSE'
cfg.0 = row - 1
/* sort all elements ascending, ignore the case */
Call SysStemSort 'cfg.', 'A', 'I'
do i = 1 to cfg.0
parse value cfg.i with .'¤'cfg.i
call Lineout ARG(1), cfg.i
end
call stream ARG(1), 'C' 'CLOSE'The second script is very similar but provide a more generic way to sort each row of data...something that may be useful in several situations.
Please note (See Rexx Tips & Tricks for more info about the parameters) that it's possible to specify that the stem (array) should be sorted Descending instead of Ascending and that it's possible to make the sort Case sensitive (Insensitive here).
/* ROWSORT */
call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
call SysLoadFuncs
row = 1
do while lines( ARG(1) ) > 0
cfg.row = linein( ARG(1) )
row = row + 1
end
call stream ARG(1), 'C' 'CLOSE'
cfg.0 = row - 1
/* sort all elements ascending, ignore the case */
Call SysStemSort 'cfg.', 'A', 'I'
do i = 1 to cfg.0
call Lineout ARG(1), cfg.i
end
call stream ARG(1), 'C' 'CLOSE'
//Jan-Erik