Author Topic: how to use $INCLUDE  (Read 3487 times)

0 Members and 1 Guest are viewing this topic.

Offline tuc47

  • Newbie
  • Posts: 12
    • View Profile
how to use $INCLUDE
« on: September 13, 2019, 01:45:47 pm »
Can someone give some examples on how to use the $INCLUDE command?
How does it work?

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: how to use $INCLUDE
« Reply #1 on: September 13, 2019, 01:56:26 pm »
'$INCLUDE:'thisfile.bi'

and it brings what ever is in the file into your code, so you do have pay attention to where you put it, if it contains SUB or FUNCTIONs then it needs to be placed at the bottom of your code, if it has TYPEs, DIMs, CONSTs, or DATA it should be at the start of your code. but thats about it.
Granted after becoming radioactive I only have a half-life!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: how to use $INCLUDE
« Reply #2 on: September 13, 2019, 02:06:48 pm »
It's easy. First to understand a program without $INCLUDE, then the same program with $INCLUDE:

Without:

Code: QB64: [Select]
  1.     LOCATE 1
  2.     PRINT "Today is "; Get_Date$; " "; Get_Time$
  3.  
  4.  
  5.  
  6. FUNCTION Get_Time$
  7.     Get_Time$ = TIME$
  8.  
  9. FUNCTION Get_Date$
  10.     Get_Date$ = DATE$
  11.  
  12.  


and with - hide functions using $INCLUDE to file, let say to  time.bm file:

Code: QB64: [Select]
  1.     LOCATE 1
  2.     PRINT "Today is "; Get_Date$; " "; Get_Time$
  3.  
  4. '$include:'time.bm'
  5.  

If must INCLUDE be used on top or in end in program depends on what included files contains. Contains DIM SHARED variables? Place it to begin. Contains SUBs or Functions? Place it to end.
« Last Edit: September 13, 2019, 02:08:56 pm by Petr »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: how to use $INCLUDE
« Reply #3 on: September 13, 2019, 02:35:19 pm »
As Petr shows, the .BM file goes at the very bottom and contains the actual subs and functions, whereas a .BI would go at the top and contain CONSTs, TYPEs or SHARED variables.

Unlike QB45, you don't have to DECLARE subs and functions in the .BI file if they are QB64 code.

Here is another example the .BM file for Float Array Tools.BM (I just commented out the test code to rename as .BM)
Code: QB64: [Select]
  1. ' Floats Array Tools.BM
  2. ''OPTION _EXPLICIT
  3. '' Build a set of Floats Array tools for handling Tomaaz challenge
  4. '' Build Floats Array Tools.bm from these
  5. 'RANDOMIZE TIMER
  6. 'DIM i AS LONG, test$, TomaazTest$
  7. 'TomaazTest$ = "120 135 345 345 1890 12 120 12 135 712 78 120"
  8. ''FOR i = 1 TO 500
  9. ''    test$ = test$ + LTRIM$(STR$(INT(RND * 100))) + " "
  10. ''NEXT
  11. ''test$ = RTRIM$(test$)
  12. 'test$ = TomaazTest$
  13. 'PRINT "Test string: "; test$
  14.  
  15. 'REDIM temp(0) AS _FLOAT
  16. 'Split2Floats test$, " ", temp()
  17. 'uniqueFloats temp()
  18. 'qSortFloats LBOUND(temp), UBOUND(temp), temp()
  19. 'reverseFloats temp()
  20. 'PRINT JoinFloats$(temp(), 0, 10, " ")
  21.  
  22.  
  23.  
  24. 'a() must be initialized as redim a(lb to ub)
  25. SUB uniqueFloats (a() AS _FLOAT) 'make all the items in the a array unique like a proper set
  26.     DIM i AS LONG, ti AS LONG, j AS LONG, u AS INTEGER, lba AS LONG
  27.     lba = LBOUND(a)
  28.     REDIM t(lba TO lba) AS _FLOAT 'rebuild container
  29.     t(lba) = a(lba): ti = lba
  30.     FOR i = lba + 1 TO UBOUND(a) 'for each element in array
  31.         u = -1
  32.         FOR j = lba TO ti 'check if not already in new build
  33.             IF a(i) = t(j) THEN u = 0: EXIT FOR 'oh it is unique is false
  34.         NEXT
  35.         IF u THEN 'OK add it to rebuild
  36.             ti = ti + 1
  37.             REDIM _PRESERVE t(lba TO ti) AS _FLOAT
  38.             t(ti) = a(i)
  39.         END IF
  40.     NEXT
  41.     REDIM a(lba TO ti) AS _FLOAT 'goodbye old array
  42.     FOR i = lba TO ti 'now copy the unique elements into array
  43.         a(i) = t(i)
  44.     NEXT
  45.  
  46. SUB qSortFloats (start AS LONG, finish AS LONG, a() AS _FLOAT)
  47.     DIM Hi AS LONG, Lo AS LONG, Middle AS _FLOAT
  48.     Hi = finish: Lo = start
  49.     Middle = a((Lo + Hi) / 2) 'find middle of array
  50.     DO
  51.         DO WHILE a(Lo) < Middle: Lo = Lo + 1: LOOP
  52.         DO WHILE a(Hi) > Middle: Hi = Hi - 1: LOOP
  53.         IF Lo <= Hi THEN
  54.             SWAP a(Lo), a(Hi)
  55.             Lo = Lo + 1: Hi = Hi - 1
  56.         END IF
  57.     LOOP UNTIL Lo > Hi
  58.     IF Hi > start THEN qSortFloats start, Hi, a()
  59.     IF Lo < finish THEN qSortFloats Lo, finish, a()
  60.  
  61. SUB reverseFloats (a() AS _FLOAT)
  62.     DIM i AS LONG, ti AS LONG
  63.     REDIM t(LBOUND(a) TO UBOUND(a)) AS _FLOAT
  64.     ti = LBOUND(a)
  65.     FOR i = UBOUND(a) TO LBOUND(a) STEP -1 'load t from top to bottom of a
  66.         t(ti) = a(i)
  67.         ti = ti + 1
  68.     NEXT
  69.     FOR i = LBOUND(a) TO UBOUND(a) 'reload a from t
  70.         a(i) = t(i)
  71.     NEXT
  72.  
  73. 'notes: REDIM the a(0) as _float to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
  74. SUB Split2Floats (mystr AS STRING, delim AS STRING, a() AS _FLOAT)
  75.     ' I am hoping _floats will cover any number type
  76.     ' bplus modifications of Galleon fix of Bulrush Split reply #13
  77.     ' http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
  78.     ' this sub further developed and tested here: \test\Strings\Split test.bas
  79.     DIM copy AS STRING, p AS LONG, curpos AS LONG, arrpos AS LONG, lc AS LONG, dpos AS LONG
  80.     copy = mystr 'make copy since we are messing with mystr
  81.     'special case if delim is space, probably want to remove all excess space
  82.     IF delim = " " THEN
  83.         copy = RTRIM$(LTRIM$(copy))
  84.         p = INSTR(copy, "  ")
  85.         WHILE p > 0
  86.             copy = MID$(copy, 1, p - 1) + MID$(copy, p + 1)
  87.             p = INSTR(copy, "  ")
  88.         WEND
  89.     END IF
  90.     curpos = 1
  91.     arrpos = 0
  92.     lc = LEN(copy)
  93.     dpos = INSTR(curpos, copy, delim)
  94.     DO UNTIL dpos = 0
  95.         a(arrpos) = VAL(MID$(copy, curpos, dpos - curpos))
  96.         arrpos = arrpos + 1
  97.         REDIM _PRESERVE a(arrpos + 1) AS _FLOAT
  98.         curpos = dpos + LEN(delim)
  99.         dpos = INSTR(curpos, copy, delim)
  100.     LOOP
  101.     a(arrpos) = VAL(MID$(copy, curpos))
  102.     REDIM _PRESERVE a(arrpos) AS _FLOAT
  103.  
  104. FUNCTION JoinFloats$ (a() AS _FLOAT, aStart AS LONG, aStop AS LONG, delimiter AS STRING)
  105.     DIM i AS LONG, iStart, iStop, b AS STRING
  106.     IF aStart < LBOUND(a) THEN iStart = LBOUND(a) ELSE iStart = aStart
  107.     IF aStop > UBOUND(a) THEN iStop = UBOUND(a) ELSE iStop = aStop
  108.     FOR i = iStart TO iStop
  109.         IF i = iStop THEN
  110.             b = b + LTRIM$(STR$(a(i)))
  111.         ELSE
  112.             b = b + LTRIM$(STR$(a(i))) + delimiter
  113.         END IF
  114.     NEXT
  115.     JoinFloats$ = b
  116.  

And a code file that uses the Floats array tools.BM
Code: QB64: [Select]
  1. test$ = " 10 2 3 8 9 1 5 4 7 6 6 7 1 2"
  2.  
  3. REDIM temp(0) AS _FLOAT
  4. Split2Floats test$, " ", temp()
  5. uniqueFloats temp()
  6. qSortFloats LBOUND(temp), UBOUND(temp), temp()
  7. reverseFloats temp()
  8. PRINT JoinFloats$(temp(), 0, 10, " ")
  9.  
  10. '$include: 'Floats array tools.BM'
  11.  

Ha! Today I just learned that you can unselect the just .BAS files when Opening / Loading files to IDE.

I have to say, I don't use '$INCLUDE much but just copy/paste what I need from other files, a little bit easier to view, but if you have massive code file you are working on, it is nice to have all the subs and functions that are working separate from the ones you are working in current code.
« Last Edit: September 13, 2019, 03:04:29 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: how to use $INCLUDE
« Reply #4 on: September 13, 2019, 04:36:17 pm »
Oh just FYI, the extensions BI and BM are not necessary you can use what ever you want, BAS, MAD, BST, what ever. BI and BM are just common ones you may see used around here. BI (BASIC Include), or BM(BASIC Module) or at least that is what I once read them as but again its not set in stone that you use those extensions. 

You might also check out the WIKI;

https://qb64.org/wiki/$INCLUDE
 
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: how to use $INCLUDE
« Reply #5 on: September 13, 2019, 07:24:43 pm »
Thanks Cobalt, I was wondering what BI and BM stood for.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: how to use $INCLUDE
« Reply #6 on: September 13, 2019, 07:49:48 pm »
Thanks Cobalt, I was wondering what BI and BM stood for.

Basic Initialization and Basic Modules, is how I think of them. 

The *.BI is where your DIM SHARED, CONST, TYPE and such variables all go, and the *.BM is where the SUB/FUNCTIONs all go.  Place the first at the top of the code, and the second at the bottom.  :)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!