Structures in Procedures

Just starting out? Need help? Post your questions and find answers here.
epog10
User
User
Posts: 93
Joined: Sat May 29, 2010 11:46 am
Location: UK

Structures in Procedures

Post 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)
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Structures in Procedures

Post 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.
Last edited by Demivec on Fri May 01, 2015 1:18 am, edited 2 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Structures in Procedures

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Structures in Procedures

Post 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.
DE AA EB
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Structures in Procedures

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Structures in Procedures

Post by davido »

@wilbert,
Thank you for your kind reply, it is appreciated. :D
I will take your advice and check out the links from Google.
DE AA EB
epog10
User
User
Posts: 93
Joined: Sat May 29, 2010 11:46 am
Location: UK

Re: Structures in Procedures

Post 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!
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Structures in Procedures

Post 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. :)
epog10
User
User
Posts: 93
Joined: Sat May 29, 2010 11:46 am
Location: UK

Re: Structures in Procedures

Post 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!
bohne_68
New User
New User
Posts: 8
Joined: Mon Aug 26, 2013 10:42 am
Location: Stuttgart
Contact:

Re: Structures in Procedures

Post 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 ...
---
PB 5.40 X64
Many clouds cover the sun
epog10
User
User
Posts: 93
Joined: Sat May 29, 2010 11:46 am
Location: UK

Re: Structures in Procedures

Post 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.
Post Reply