Author Topic: Demo: Getting arrays into an array of a UDT  (Read 4957 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #15 on: January 30, 2021, 04:08:57 pm »
Line 68 needs to say INTEGER.

I generally just accept the extra space usage of longs in order to get their extra range and prevent overflow, but to reach their own.

Yeah I just caught that looking over post. :)

Fixed, now I can sleep tonight! ha, ha
Code: QB64: [Select]
  1. _TITLE "Getting an Array into UDT Mod 1 Luke Method" ' started b+ 2020-01-29
  2. ' 2021-01-30   Mod 1 employ Lukes method, dump ALL previous subroutines this is much less complex
  3. '  I am getting too many screwups trying x, y, x, y, x, y pairs! So do 2 string arrays 1 for X 1 for Y.
  4. '  Still getting some screwy problem from 1st and last elements???  but works fine if I ignore.
  5. ' 2021-01-30 fixed! missed changing LONG to INTEGER in one place.
  6.  
  7. CONST Xmax = 1200, Ymax = 700
  8. TYPE ArrayType
  9.     Aname AS STRING ' here is the name of the array
  10.     X AS STRING '    here is the array joined into a long string so it may be stored in a UDT
  11.     Y AS STRING '
  12.  
  13. REDIM words(100) AS ArrayType ' record the script positions of handwritten words and save the word and positions into a UDT
  14.  
  15. SCREEN _NEWIMAGE(Xmax, Ymax, 32)
  16. _DELAY .25 'need time for screen to load before attempting to move it.
  17.  
  18. _TITLE "To gather data:  Hand write words with mouse and then enter the word, up to 100 words or escape."
  19. wi = 0 ' word index for words
  20.     CLS
  21.     mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  22.     IF mb THEN
  23.         words(wi).X = "": words(wi).Y = "": ai = 0 ' ai = array index for the elements in X, Y arrays
  24.         set words(wi).X, ai, mx: set words(wi).Y, ai, my: ai = ai + 1
  25.         PSET (mx, my)
  26.         WHILE mb
  27.             WHILE _MOUSEINPUT: WEND
  28.             mx = _MOUSEX: my = _MOUSEY: mb = _MOUSEBUTTON(1)
  29.             set words(wi).X, ai, mx: set words(wi).Y, ai, my: ai = ai + 1
  30.             LINE -(mx, my)
  31.             _LIMIT 20
  32.         WEND
  33.         CLS
  34.         INPUT "Enter word > "; w$
  35.         words(wi).Aname = w$
  36.         wi = wi + 1
  37.         IF wi > 100 THEN EXIT DO ' wi will always be 1 more than last word
  38.     END IF
  39. wi = wi - 1
  40.  
  41. ' simple playback
  42. FOR j = 0 TO wi
  43.     CLS
  44.     IF words(j).Aname <> "" THEN
  45.         PRINT "This is word "; words(j).Aname
  46.         FOR i = 0 TO LEN(words(j).X) / 2 - 1 'fixed
  47.             IF i = 0 THEN PSET (get%(words(j).X, i), get%(words(j).Y, i)) ELSE LINE -(get%(words(j).X, i), get%(words(j).Y, i))
  48.             _LIMIT 30
  49.         NEXT
  50.         _DELAY 1
  51.     END IF
  52.  
  53. '              ======================= Luke's Method ========================
  54.  
  55. SUB set (array$, index AS INTEGER, value AS INTEGER) ' <<<< having big debate with myself between Integer and Long, Integers are shorter
  56.     REDIM element_size AS INTEGER
  57.     element_size = LEN(value)
  58.     IF LEN(array$) < element_size * (index + 1) THEN
  59.         array$ = array$ + STRING$(element_size * (index + 1) - LEN(array$), CHR$(0))
  60.     END IF
  61.     MID$(array$, index * element_size + 1) = _MK$(INTEGER, value) '< fixed
  62.  
  63. FUNCTION get% (array$, index AS INTEGER)
  64.     get% = _CV(INTEGER, MID$(array$, index * 2 + 1, 2))
  65.  
  66.  
  67.  
« Last Edit: January 30, 2021, 04:29:41 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #16 on: January 30, 2021, 04:26:50 pm »
That is so cool aplus . mega beyond what I had in mind but oh so cool.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #17 on: January 30, 2021, 04:30:51 pm »
That is so cool aplus . mega beyond what I had in mind but oh so cool.

Well I am glad I tried this because I got a nice lesson myself!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #18 on: January 30, 2021, 05:28:06 pm »
This looks interesting, but I'm too bogged down at the moment to get sidetracked. I hope to revisit it soon.

I also love the shameless advertising of marking your own posts as the Best Answer. Keep in mind, as per our previous conversation,I'm more than happy to do that for you, but my fee is my fee! :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #19 on: January 30, 2021, 06:05:21 pm »
This looks interesting, but I'm too bogged down at the moment to get sidetracked. I hope to revisit it soon.

I also love the shameless advertising of marking your own posts as the Best Answer. Keep in mind, as per our previous conversation,I'm more than happy to do that for you, but my fee is my fee! :D

Pete

I just came back to change that, Luke's is better description!

I do have a question about Len(get) in this:
Code: QB64: [Select]
  1. FUNCTION get& (array$, index)
  2.     get = _CV(LONG, MID$(array$, index * LEN(get) + 1, LEN(get)))
  3.  

That really work? using the function name most curious!
Why yes, yes it does work in demo. ;-)) Before now I just read the code never tried the Run.

« Last Edit: January 30, 2021, 06:14:13 pm by bplus »

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #20 on: January 30, 2021, 06:21:27 pm »
I do have a question about Len(get) in this:
Code: QB64: [Select]
  1. FUNCTION get& (array$, index)
  2.     get = _CV(LONG, MID$(array$, index * LEN(get) + 1, LEN(get)))
  3.  

That really work? using the function name most curious!

I'm shamelessly exploiting a QB64 bug where the name of a function inside that function is treated as a regular variable, not a recursive call. I'm pretty sure you can't do that in QBasic, but of course it's not integral to the program - just a convenient way to reduce the number of things to change if you change the data type.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Demo: Getting arrays into an array of a UDT
« Reply #21 on: January 31, 2021, 12:00:24 pm »
I have worked up 3 pairs of SUBs and FUNCTIONs for using Integers (Long), Floats (Double) and Strings (length Limit 32 characters) Array like strings with little demos of each:

https://www.qb64.org/forum/index.php?topic=1511.msg129084#msg129084

Find a great idea and you've got to run with it a bit! These should be useful for UDT "arrays" at least.

BTW I have SetLong down to this 2 liner:
Code: QB64: [Select]
  1. SUB SetLong (array$, index AS LONG, value&) ' Luke's Method except option explicit requires mod, no variables needed for one type
  2.     IF LEN(array$) < 4 * (index + 1) THEN array$ = array$ + STRING$(4 * (index + 1) - LEN(array$), CHR$(0))
  3.     MID$(array$, index * 4 + 1) = _MK$(LONG, value&)
« Last Edit: January 31, 2021, 12:09:03 pm by bplus »