Author Topic: Equivalent of PB ARRAY SORT statement  (Read 1800 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
Equivalent of PB ARRAY SORT statement
« on: March 27, 2018, 12:47:12 PM »
Hello

is there an O2 equivalent of the ARRAY SORT statement ?   according to http://manmrk.net/tutorials/basic/PowerBASIC/pbcc/array_sort_statement.htm

Code: [Select]
Sort all or part of a given array.

Syntax :

Numeric array:

ARRAY SORT darray([index]) [FOR count] [,TAGARRAY tarray()] [,{ASCEND | DESCEND}]



String array:

ARRAY SORT dArray([index]) [FOR count] [,FROM start TO end] [,COLLATE {UCASE |

    cstring}] [,TAGARRAY tarray()] [,{ASCEND | DESCEND}]

Remarks


ARRAY SORT sorts all or part of darray, an n-dimensional array, in ascending or descending order.  tarray is a tag-along array whose elements are swapped in the same order as those in darray as the sort proceeds (you could sort an array of names and have an array of corresponding addresses tag along, for example).  tarray must have at least as many elements as darray, since corresponding elements of tarray will be swapped during the sort.


Charles Pegge

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #1 on: March 28, 2018, 05:44:19 AM »
Hi Chris,

We have something similar, but more flexible in generics.inc.

NewSortIndex enables you to create indexes on any kind of data arrays, filtered by any criteria and sorted by any criteria. I think this is a better approach for handling sorts in a data-processing application.

I've added another macro to generics: NewSortedData to create a new array of data from the original data using the index.

Code: [Select]
'2018-03-28 T 11:03:11
'FILTERING AND SORTING
uses generics
uses console

'CREATE SOME DATA
=================
string dat={"a2","a1","a","b","c","d","e","f","g","h","i","j"}
int ct = CountOf dat
int cf 'Count of Filtered Data

'CALLBACK MACROS FOR FILTERING AND INDEXING

macro filter(accept,i)
======================
if asc(dat[i])<=0x65 then accept=1
end macro

macro compare(swap,a,b)
=======================
if dat[a]<dat[b] then swap=1 'DESCENDING ORDER
end macro

'
NewSortIndex idx,ct,cf,filter,compare


'SHOW RESULTS
=============
print "RAW DATA" cr
ListArray dat,print,ct
printl
print "FILTERED AND INDEXED DATA" cr
ListArray dat,print,cf,idx
printl
print "INDEX USED" cr
ListArray idx,print,cf

NewSortedData(sd,dat,idx,cf)
printl
print "SORTED DATA" cr
ListArray sd,print,cf
wait
DelSortIndex idx
DelSortedData sd
« Last Edit: March 28, 2018, 09:12:06 PM by Charles Pegge »

chrisc

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #2 on: March 28, 2018, 10:29:57 AM »
Thanxx Charles

but when i modified the program as below, to sort the array in ascending order
 it display a screen of elements that have blanks from index 1 to 7 as shown in the atteched screen shot?

i also added 2 new macros filterblanks  and compareAO


how to eliminate these blank elements in the screen display?

Code: [Select]
'2018-03-28 T 11:03:11
'FILTERING AND SORTING
' http://www.oxygenbasic.org/forum/index.php?PHPSESSID=bbb1n9397c7uq6d0f1ft4vscr0&topic=1613.msg17561;topicseen#msg17561
' Sort_Array.o2bas

' NewSortIndex enables you to create indexes on any kind of data arrays,
' filtered by any criteria and sorted by any criteria.


uses generics
uses console

'CREATE SOME DATA
=================
string dat={"a2","a1","a","b","c","d","e","f","g","h","i","j"}
int ct = CountOf dat
int cf 'Count of Filtered Data

'CALLBACK MACROS FOR FILTERING AND INDEXING


'  filter off those characters  > than e
macro filter(accept,i)
======================
if asc(dat[i])<=0x65 then accept=1
end macro


' filter off blank characters
macro filterblanks(accept,i)
======================
if asc(dat[i])<>0x22 then accept=1
end macro


'DESCENDING ORDER macro
macro compareDO(swap,a,b)
=======================
if dat[a]<dat[b] then swap=1
end macro


'ASCENDING ORDER macro
macro compareAO(swap,a,b)
=======================
if dat[a]>dat[b] then swap=1
end macro


'------------------------------------------------------
' Main program starts here
' descending order
NewSortIndex idx,ct,cf,filter,compareDO



'SHOW RESULTS
=============
print "RAW DATA" cr
ListArray dat,print,ct
printl
print "FILTERED AND INDEXED DATA" cr
ListArray dat,print,cf,idx
printl
print "INDEX USED" cr
ListArray idx,print,cf

NewSortedData(sd,dat,idx,cf)
printl
print "SORTED DATA   descending" cr
ListArray sd,print,cf

'DelSortIndex idx
'DelSortedData sd


' Now in Ascending order
NewSortIndex idx,ct,cf,filterblanks,compareAO


NewSortedData(sd,dat,idx,cf)
printl
print "SORTED DATA   Ascending" cr
ListArray sd,print,cf


wait
DelSortIndex idx
DelSortedData sd





chrisc

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #3 on: March 28, 2018, 12:00:17 PM »
sometimes i get this message upon exiting an O2 compile exe program
it looks like it is caused by abrupt exit from a program

see attached screen shot

any idea how to resolve this issue,  is there an O2 command to enable proper exit of a program?

Charles Pegge

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #4 on: March 28, 2018, 03:19:50 PM »
Oops! A sys which should have been an int, causing 64bit misbehaviour.

My fixed generics attached.

o2 has a program epilog to clean-up before exiting, and should always be left to do its own thing.

« Last Edit: March 28, 2018, 09:11:15 PM by Charles Pegge »

chrisc

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #5 on: March 28, 2018, 07:51:49 PM »
Hi Charles
still having the same issue after having downloaded the latest Oxygen Progress and use the new generics.inc
still have the bad_module_info error message when exiting.
looks like something didn't run right

Charles Pegge

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #6 on: March 28, 2018, 09:10:36 PM »
Hi Chris,

cf required resetting to 0

Code: [Select]
' Now in Ascending order
cf=0
NewSortIndex idx,ct,cf,filterblanks,compareAO

but that can be done from NewSortIndex. I have also improved memory management so that the macros are safe to use repetitively inside a looping structure, automatically deleting old indexes before creating new ones.




chrisc

  • Guest
Re: Equivalent of PB ARRAY SORT statement
« Reply #7 on: March 28, 2018, 10:17:39 PM »
Thanxx  a lot Charles,  no more problem now upon exit