Page 1 of 1
Structures in Procedures
Posted: Wed Apr 29, 2015 1:56 pm
by epog10
Wanting to sort a list for printing, I found that sorting alphanumeric numbers does not work very well. I therefore started looking at the help files for other ways of doing it.
Thus I discovered Structures and SortStructuredArray which are very new to me!
The various examples work well. However I cannot seem to be able to 'convert' them correctly for use within a procedure. Neither have I found a clear-cut example of this. Perhaps someone can point me in the right direction or correct the following code. I think that the problem lies with the ??????????? bit in that I cannot work out how to identify the structure as an array, correctly.
In the following the structure consists of textual or numeric values with iPOINT indicating the list line. Loading and sorting is based on the column predefined in iCOL. All variables have been globally pre-declared.
Code: Select all
Structure HOLDER
aTEXT.s
fVALUE.f
iPOINT.i
EndStructure
Dim HOLDERS.HOLDER(1000) ;Array for sorting by name or value (up to 1000 list lines)
Procedure SORTALL(Array ???????????) ;Load/sort by column number in iCOL
X = CountGadgetItems(#Listview_0) ;Trap length of exiting list
For iINDEX = 0 To X - 1 ;Grab required items To array
Select iCOL
Case 1
HOLDERS(1)\aTEXT = "Here it is." ;Test line!!
HOLDERS(1)\iPOINT = 123 ;Test line!!
; HOLDERS(0)\aTEXT = Trim(aNAME)
; HOLDERS(0)\iPOINT = iINDEX
Case 2
HOLDERS(iINDEX)\fVALUE = fPRICE : HOLDERS(iINDEX)\iPOINT = iINDEX
;------------ and so on with a sort taking place after the array load
EndProcedure
..... and later in program body:-
SORTALL(HOLDERS)
Re: Structures in Procedures
Posted: Wed Apr 29, 2015 2:56 pm
by Demivec
Code: Select all
Procedure SORTALL(Array HOLDERS.HOLDER(1)) ;'1' indicates the # of dimensions if there were 2 dimensions (x,y) it would be '2'
This is how to declare the array as a parameter, any changes to the array affect the array you passed in (by reference) and they both must be of the same structure and have the same number of dimensions (though they may have different names).
It looks like you are going to be filling the array in the procedure. You can dimension it to the correct size (instead of keeping it at a constant 'max' size) first by using 'Dim HOLDERS.HOLDER(X - 1)' where X is the count of the gadget items.
I hope this helps. I'm not clear of how you want to sort things from there as 'alpha-numeric' sorting covers a range of methods.
Re: Structures in Procedures
Posted: Wed Apr 29, 2015 4:57 pm
by wilbert
Not really an answer to your question but if you don't need a stable sort, you could consider qsort.
It allows you to create your own sort function. Here's an example of sorting numeric strings.
Code: Select all
ImportC ""
qsort(*base, num, size, *comparator)
EndImport
Dim values.s(5)
values(0) = "40"
values(1) = "10"
values(2) = "100"
values(3) = "90"
values(4) = "20"
values(5) = "25"
ProcedureC.i Compare(*a.String, *b.String)
ProcedureReturn Val(*a\s) - Val(*b\s)
EndProcedure
qsort(@values(), ArraySize(values()) + 1, SizeOf(String), @Compare())
For n = 0 To 5
Debug values(n)
Next
Re: Structures in Procedures
Posted: Wed Apr 29, 2015 9:20 pm
by davido
@wilbert,
Probably a dumb question, but nevertheless could you please explain how this works.
I checked the manual but could not find, or missed, the explanation there.
I was a bit surprised that it worked on both Mac and PC.
Re: Structures in Procedures
Posted: Wed Apr 29, 2015 10:02 pm
by wilbert
davido wrote:@wilbert,
Probably a dumb question, but nevertheless could you please explain how this works.
I checked the manual but could not find, or missed, the explanation there.
I was a bit surprised that it worked on both Mac and PC.
It's a C library function (that's why you need ImportC) which is available on all three systems (Windows, Mac and Linux).
If you google for "qsort stdlib" you get lots of links with information.
Re: Structures in Procedures
Posted: Thu Apr 30, 2015 8:59 pm
by davido
@
wilbert,
Thank you for your kind reply, it is appreciated.
I will take your advice and check out the links from Google.
Re: Structures in Procedures
Posted: Fri May 01, 2015 12:07 pm
by epog10
Thank you for your responses. I am no longer getting errors from the Procedure!
HOWEVER I am struggling in creating the call to the procedure (the last line of the above code). Whatever arrangement I try comes up with a failure in the form of
"xxxxx is not a function, array, list, map or macro". I really think I am getting too old to learn new tricks!
Confused by what I am trying to do? Yes, I did not make it very clear and I also missed one line when I transcribed and cleaned-up the code.
It should be:-
Code: Select all
For iINDEX = 0 To X - 1 ;Grab required items To array
LISTSPLIT()
Select iCOL
LISTSPLIT breaks up the list line into its components. Some are alphanumeric characters, some real numbers, some dates.
Initally I had a simple string array and this works well for the text elements and the dates in the format YY/MM/DD. However real numbers can only be handled as characters, so the sort gives: 1, 2, 222, 3, 4 etc.. Hence my search for a different approach.
I have noted the C library for another day but on this excercise I really think I should complete enough of my knowledge to be able to use Structures!
Re: Structures in Procedures
Posted: Fri May 01, 2015 12:32 pm
by Demivec
epog10 wrote:HOWEVER I am struggling in creating the call to the procedure (the last line of the above code). Whatever arrangement I try comes up with a failure in the form of "xxxxx is not a function, array, list, map or macro".
Code: Select all
SORTALL(HOLDERS()) ;Use a pair of empty parentheses after the array name.
epog10 wrote:Initally I had a simple string array and this works well for the text elements and the dates in the format YY/MM/DD. However real numbers can only be handled as characters, so the sort gives: 1, 2, 222, 3, 4 etc.. Hence my search for a different approach.
That makes things a bit more clear.

Re: Structures in Procedures
Posted: Sat May 02, 2015 11:33 am
by epog10
I had already tried 'SORTALL(HOLDERS())' and it had not worked. With your reassurance that it was the right format, I played about with it.
All working now - it does not like being called from within a Procedure. It has not been a problem to arrange for the sort call to be made from the body of the program and then call the print procedure.
I guess it makes some sense - in the case of structures, these probably need to be 'handed down' from procedure to procedure.
So many thanks for your help. I now have a template for the implementation of structures even if the understanding is not quite complete!
Re: Structures in Procedures
Posted: Sat May 02, 2015 3:22 pm
by bohne_68
Do you tried the internal sort-functions?
- SortStructuredArray()
- SortStructuredList()
In my cases it works wery well. With the structure described it should work very well ...
Re: Structures in Procedures
Posted: Sat May 02, 2015 10:46 pm
by epog10
Yes, I am using the SortStructuredArray()
The problem I had was in accessing the sort routine in order to load the structure-based array and then sort it.
All finished now.