October 30, 2025, 10:33:51 AM

News:

IWBasic runs in Windows 11!


UDT Question

Started by billhsln, April 22, 2008, 11:52:05 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

If I define a structure as:

TYPE xxx
  DEF NAME[30]:ISTRING
  DEF CAREOF[30]:ISTRING
  DEF ADDR[30]:ISTRING
  DEF CITY[30]:ISTRING
  DEF STATE[2]:ISTRING
  DEF ZIP[9]:ISTRING
ENDTYPE

1) Can I define the variable as:

DEF CUSTOMER[100]:xxx

2) If it can be defined as above, how can or are the fields referenced?

Like:

CUSTOMER.NAME
  • = "John Doe"
    CUSTOMER.CAREOF[0] = " "
    CUSTOMER.ADDR[0] = "1313 Mockingbird Lane"
    CUSTOMER.CITY
  • = "Los Angeles"
    CUSTOMER.STATE[0] = "CA"
    CUSTOMER.ZIP
  • = "91800"

    CUSTOMER.NAME[1] = "Jane Smith"

    etc...???


    Thanks,
    Bill
When all else fails, get a bigger hammer.

Barney

I will never understand why you people ask questions like this when you have the compiler at your fingertips. Just type your ideas in and try them out.

Anyways... This is how it should look like in order to work properly.

OPENCONSOLE

TYPE xxx
  DEF NAME[30]:ISTRING
  DEF CAREOF[30]:ISTRING
  DEF ADDR[30]:ISTRING
  DEF CITY[30]:ISTRING
  DEF STATE[2]:ISTRING
  DEF ZIP[9]:ISTRING
ENDTYPE

DEF CUSTOMER[100]:xxx

CUSTOMER[0].NAME = "John Doe"
CUSTOMER[0].CAREOF = " "
CUSTOMER[0].ADDR = "1313 Mockingbird Lane"
CUSTOMER[0].CITY = "Los Angeles"
CUSTOMER[0].STATE = "CA"
CUSTOMER[0].ZIP = "91800"

CUSTOMER[1].NAME = "Jane Smith"

PRINT CUSTOMER[0].NAME," - ",CUSTOMER[1].NAME

DO:UNTIL INKEY$ <> ""
CLOSECONSOLE


Barney

billhsln

All I was looking for was 1) Yes and 2) CUSTOMER[0].Name = "Jones".  I needed the info because I had never seen a dimensioned UDT defined or referenced. I had tried what I wrote up and it did not work.  Did not know to reference the the first part as a dimensioned variable.  I don't even remember seeing it in the manual.  I can now use this info to be able to sort a UDT.

So, thanks for the reply.

Bill
When all else fails, get a bigger hammer.

Ionic Wind Support Team


OPENCONSOLE
'example of using C qsort to sort an array of UDT's and then C bsearch to search them.
'for sake of convenience the array is created by using DATA statements
'Both qsort and bsearch require your compare function to be CDECL
DECLARE CDECL EXTERN _qsort(base as POINTER,num as INT,size as INT,compfunc as UINT)
DECLARE CDECL EXTERN _bsearch(key as POINTER,base as POINTER,num as INT,size as INT,compfunc as UINT),POINTER
DECLARE CDECL myCompare(elem1 as POINTER,elem2 as POINTER),INT
TYPE myType
DEF key[40] as ISTRING
DEF clr[10] as ISTRING
DEF movie[40] as ISTRING
ENDTYPE
'create the array to be sorted and searched.
DEF array[10] as mytype
'a pointer for bsearch
DEF pFound as POINTER
FOR x = 1 TO 10
   GETDATA favorites,array[x-1].key
   GETDATA favorites,array[x-1].clr
   GETDATA favorites,array[x-1].movie
NEXT x
'sort the array
_qsort(array,10,LEN(myType),&myCompare)
'display the sorted results
PRINT "Sorted results..."
FOR x = 1 TO 10
   PRINT "Name: ",array[x-1].key
   PRINT "Favorite Color: ",array[x-1].clr
   PRINT "Favorite Movie: ",array[x-1].movie:PRINT
NEXT x
PRINT "................."
'find some elements with bsearch
PRINT "Searching for 'Joe'"
pFound = _bsearch("Joe",array,10,LEN(myType),&myCompare)
IF pFound <> NULL
PRINT "Found 'Joe'"
PRINT "Favorite Color: ",#<myType>pFound.clr
PRINT "Favorite Movie: ",#<myType>pFound.movie:PRINT
ENDIF
PRINT "Searching for 'Vikki'"
pFound = _bsearch("Vikki",array,10,LEN(myType),&myCompare)
IF pFound <> NULL
PRINT "Found 'Vikki'"
PRINT "Favorite Color: ",#<myType>pFound.clr
PRINT "Favorite Movie: ",#<myType>pFound.movie:PRINT
ENDIF
PRINT "any key to close"
DO:UNTIL INKEY$ <> ""
END
'our subroutine to compare elements on the array
'remember to use DECLARE CDECL to declare it.
SUB myCompare(elem1 as POINTER,elem2 as POINTER),INT
   'qsort and bsearch will call this function
    'to compare array elements
   'RETURN < 0 if elem1 less than elem2
    'RETURN 0 if elem1 equivalent to elem2
   'RETURN > 0 elem1 greater than elem2
   'in our case we are sorting by the 'key' member of our UDT
    IF #<myType>elem1.key < #<myType>elem2.key THEN RETURN -1
   IF #<myType>elem1.key > #<myType>elem2.key THEN RETURN 1
RETURN 0
ENDSUB
DATABEGIN favorites
DATA "John","Red","Terminator"
DATA "George","Purple","The color of money"
DATA "Adam","Green","The Patriot"
DATA "Lisa","Black","Fried Green Tomatoes"
DATA "Tammy","Pink","Debbie Does Dallas"
DATA "Vikki","Blue","Signs"
DATA "Gary","Yellow","Lassie come home"
DATA "Mark","Lavender","Blown Away"
DATA "Joe","Beige","Jimmy Neutron"
DATA "Nancy","White","Down Periscope"
DATAEND
Ionic Wind Support Team

billhsln

Thanks for the sort routine ;D, will make it extremely easy.

Thanks,
Bill
When all else fails, get a bigger hammer.