Author Topic: Multiple Dimensional Array v's Type Statement  (Read 6020 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Multiple Dimensional Array v's Type Statement
« on: December 22, 2018, 09:55:35 am »
I found I just like the looks of Select Case over If.. statements and where possible I prefer to nest an IF statement in a Select Case .. just for the look of it. Now I'm writing a larger program with a lot more saving and retrieving from files. Again, just because I liked the look of it I have been using Type Statements rather than multi dimensional arrays. It's not too late for me to rethink, and recode for better efficiency - as my program gets larger and data handling increases, is there any particular advantage the array would have over the Type?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #1 on: December 22, 2018, 05:25:34 pm »
I'd say the main advantage (and maybe the only) of Type is making your code readable and understandable 3 years from now.

You also have a 3rd option, maybe considered the most primitive but still very readable and that is to have a separate single dimension array for what would be each line in the Type definition and a common index would keep all the values in sync.

A multi-dimensional array would be my last choice but it is a matter of preference.

Append: TYPE has more advantages, excellent for fixed record size for data basing and with Type you can have an array of mixed typed variables. With a multi-dimensional array, you are stuck with one type.
« Last Edit: December 23, 2018, 11:09:33 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #2 on: December 23, 2018, 11:06:59 am »
Thanks. and Merry Christmas, Happy New Year, Happy Birthday, Happy Halloween and all those other happy events I have missed. I hadn't considered going to much smaller uni-arrays, so to speak. Back to the drawing board.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #3 on: December 23, 2018, 11:11:39 am »
I appended two more advantages with a Type Array. Season's Greetings back to you!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #4 on: December 23, 2018, 05:18:41 pm »
I have to agree with Bplus on the readability of the TYPE over a multidimensional array. I feel like I have half a chance of figuring out what I was doing later on. Even so, with a good comment section to lay out the scheme, it wouldn't be too hard to get up to speed again.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #5 on: December 23, 2018, 05:56:06 pm »
I appreciate that Andy. TYPE without the comments does give a better "look" to the logical flow, and with the comment section hopefully you can pass your code onto the grand kids and they may have some chance to understand where you were going with it all. I'm just now working on breaking up an old TYPE statement into the various individual, singular arrays. Lot more 'commenting' seems to be needed as I'm losing track of which array connects with which other array - had to rename them with a common theme.

FellippeHeitor

  • Guest
Re: Multiple Dimensional Array v's Type Statement
« Reply #6 on: December 23, 2018, 06:21:35 pm »
An array of a custom type is much more readable and kind of resemble modern languages' classes.

In the end all options are indeed valid code and it boils down to personal preference.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #7 on: December 24, 2018, 02:51:17 pm »
I'd say the main advantage (and maybe the only) of Type is making your code readable and understandable 3 years from now.

You also have a 3rd option, maybe considered the most primitive but still very readable and that is to have a separate single dimension array for what would be each line in the Type definition and a common index would keep all the values in sync.

A multi-dimensional array would be my last choice but it is a matter of preference.

Append: TYPE has more advantages, excellent for fixed record size for data basing and with Type you can have an array of mixed typed variables. With a multi-dimensional array, you are stuck with one type.

another advantage - is if your a text gamer (like me hehehe), makes game saves a breeze! Just stuff player with all vars relating to player and then if needed, all dynamic game vars in a UTD, and then save both to a file..
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #8 on: December 25, 2018, 03:26:17 pm »
xra7en - That's interesting. I haven't really worked with UTD, as most of my arrays are working with values. Doesn't UTD work mostly with strings? Have to confess I did come across UTD a while ago and came away with thinking it mostly stores and retrieves words. I guess a value can be STR$'d

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #9 on: December 25, 2018, 03:45:08 pm »
UTD?

I've heard of UDT (User Defined Type) but not UTD??

Probably mean UDT given subject  of this thread.  ;-))
« Last Edit: December 25, 2018, 03:49:58 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Multiple Dimensional Array v's Type Statement
« Reply #10 on: December 25, 2018, 04:15:31 pm »
UTD?

I've heard of UDT (User Defined Type) but not UTD??

Probably mean UDT given subject  of this thread.  ;-))

I’ve worked with some databases that used UDT as an abbreviation for User Data Type, which is what I assumed it was representing here; though you’re probably correct in assuming it was originally just a typo.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #11 on: December 26, 2018, 12:12:39 pm »
My apologizes, my mind went to Universal Text Data.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #12 on: December 26, 2018, 02:49:34 pm »
UTD?

I've heard of UDT (User Defined Type) but not UTD??

Probably mean UDT given subject  of this thread.  ;-))

I’ve worked with some databases that used UDT as an abbreviation for User Data Type, which is what I assumed it was representing here; though you’re probably correct in assuming it was originally just a typo.

dyslexia...
but I mean what you know...
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #13 on: December 26, 2018, 02:55:24 pm »
its kdnda lkie redaing tihs mesasge. If you go wtih the folw, you knida get the gist of waht I was tyring to say LOL

(old  trick on how peoples minds can be when reading. we just fill in the gaps)




I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Multiple Dimensional Array v's Type Statement
« Reply #14 on: December 26, 2018, 09:05:53 pm »
its kdnda lkie redaing tihs mesasge. If you go wtih the folw, you knida get the gist of waht I was tyring to say LOL

(old  trick on how peoples minds can be when reading. we just fill in the gaps)

Code: QB64: [Select]
  1. _TITLE "Scampling" 'B+ translated from JB to QB64 only changingmsg$() and adding this line,  2019-12-26
  2. 'scampling.txt for JB v1.01 [B+=MGA] 2016-05-15
  3. 'modified for JB from scampling.bas SmallBASIC 2015-09-02 MGA/B+
  4.  
  5. '2018-12-27 need Randomize
  6. '2018-12-27 oops missed RND(0)
  7. PRINT RND(0) ' always returns the last value returned
  8.  
  9.  
  10. DIM msg$(3)
  11. 'see if scample$ can unscramble some of following by accident
  12. msg$(1) = "its kdnda lkie redaing tihs mesasge. If you go wtih the folw, you knida get the gist of waht I was tyring to say LOL"
  13. msg$(2) = "(old  trick on how peoples minds can be when reading. we just fill in the gaps) "
  14. msg$(3) = "THIS MESSAGE SHOWS YOU, THAT THE CAPABILITIES OF YOUR BRAIN ARE REALLY GREAT!"
  15.  
  16. punc$ = " <,>.?/:;'{[}]|\!@#$%^&*()_-+=" + CHR$(34)
  17.  
  18. FOR m = 0 TO UBOUND(msg$)
  19.     lastp = 0: build$ = ""
  20.     FOR i = 1 TO LEN(msg$(m))
  21.         IF INSTR(punc$, MID$(msg$(m), i, 1)) THEN
  22.             wrd$ = MID$(msg$(m), lastp + 1, i - lastp - 1)
  23.             build$ = build$ + scample$(wrd$) + MID$(msg$(m), i, 1)
  24.             lastp = i
  25.         END IF
  26.     NEXT
  27.     IF lastp < LEN(msg$(m)) THEN
  28.         wrd$ = MID$(msg$(m), lastp + 1)
  29.         build$ = build$ + scample$(wrd$)
  30.     END IF
  31.     PRINT build$
  32.  
  33. FUNCTION scample$ (wrd$)
  34.     IF wrd$ = "scramble" THEN scample$ = CHR$(34) + "scample" + CHR$(34): EXIT FUNCTION
  35.     IF wrd$ = "a" THEN scample$ = "aa": EXIT FUNCTION
  36.     IF LEN(wrd$) THEN
  37.         flaglower = 0
  38.         FOR i = 1 TO LEN(wrd$)
  39.             IF INSTR("abcdefghijklmnopqrstuvwxyz", MID$(wrd$, i, 1)) THEN flaglower = 1: EXIT FOR
  40.         NEXT
  41.         IF flaglower THEN 'just mess up the word a little, leave first and last letter the same
  42.             IF LEN(wrd$) > 3 THEN
  43.                 lp = INT(RND * (LEN(wrd$) - 3)) + 2
  44.                 wrd$ = MID$(wrd$, 1, lp - 1) + MID$(wrd$, lp + 1, 1) + MID$(wrd$, lp, 1) + MID$(wrd$, lp + 2)
  45.             END IF
  46.         ELSE 'all upper case letters so do a different substition numbers for letters and vice versa
  47.             bld$ = ""
  48.             FOR ch = 1 TO LEN(wrd$)
  49.                 L$ = MID$(wrd$, ch, 1)
  50.                 SELECT CASE L$
  51.                     CASE "A": s$ = "4"
  52.                     CASE "4": s$ = "A"
  53.                     CASE "B": s$ = "8"
  54.                     CASE "8": s$ = "B"
  55.                     CASE "E": s$ = "3"
  56.                     CASE "3": s$ = "E"
  57.                     CASE "G": s$ = "6"
  58.                     CASE "6": s$ = "G"
  59.                     CASE "I": s$ = "1"
  60.                     CASE "1": s$ = "I"
  61.                     CASE "O": s$ = "0"
  62.                     CASE "0": s$ = "O"
  63.                     CASE "S": s$ = "5"
  64.                     CASE "5": s$ = "S"
  65.                     CASE ELSE: s$ = L$
  66.                 END SELECT
  67.                 bld$ = bld$ + s$
  68.             NEXT
  69.             wrd$ = bld$
  70.         END IF
  71.     END IF
  72.     scample$ = wrd$
  73.  
  74.  

EDIT: Oops, I forgot JB handles RND differently than QB64, code has been updated.
« Last Edit: December 27, 2018, 11:19:37 am by bplus »