Listing of program p60_qsort.cgi
#! /usr/local/bin/regina
/* A quick Sort Example */
/*-----------------------------------------------------------------*/
/* This program displays a file's content (name + quantity), */
/* either sorted by name, or by quantity */
/* The file path is a constant in the program */
/* The sort criteria is passed as an argument ("name" or "qty"), */
/* and defaults to "name". */
/*-----------------------------------------------------------------*/
call setdll /* loads the HHNS shared library */
/*-- this will initialize all pre-defined REXX CGI Variables ----*/
call CgiInit "TITLE='List of REXX variables at entry in a CGI' BGCOLOR=FFFFFF"
/*---- read the file in a stem -------------------*/
fileid = "sample.txt" /* col 1-30=name, 31-43=qty */
do i = 1 by 1 while chars(fileid) > 0
t.i = linein(fileid)
end
t.0 = i - 1 /* number of lines */
/*---- sort it ! ---------------------------------*/
parse arg sort_criteria /* passed as an argument */
if sort_criteria = "qty" then cols = "31 13";
else do; cols = "1 30"; sort_criteria = "name"; end
start_sort = time('e')
call qsort cols,1,t.0
end_sort = time('e')
/*---- show it ---------------------------------*/
elaps = end_sort - start_sort /* in sssssss.uuuuuu" */
elaps = format(elaps, 2, 3)
say tblHdr( "Line #", ,
" "cgiHref("p60_qsort.cgi?name", "Name"), ,
" "cgiHref("p60_qsort.cgi?qty", "Qty") )
do i = 1 to t.0
parse var t.i name qty
say tblRow(i, "'"name"'", qty)
end
call cgiEnd
return 0
qsort : procedure expose t.
/*------------------------------------------------*/
/*--- built from HOARE's Quick Sort Algorithm ----*/
/*------------------------------------------------*/
arg kstart klen, from, to
i = from; j = to;
p = (from+to) % 2
pkey = substr(t.p, kstart, klen)
wrk = t.i; t.i = t.j; t.j = wrk
end
j = j - 1; i = i + 1
end
end
return
Back Home