Author Topic: Option Base  (Read 3154 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Option Base
« on: May 21, 2019, 02:45:27 pm »
I'm running into a situation where I think OPTION BASE 1 may help however the wiki seems to suggest that OPTION BASE affects ALL the Dimensioned arrays. Is there a way to have it apply to only 1 of many arrays? It doesn't look like it can be contained within a subroutine.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Option Base
« Reply #1 on: May 21, 2019, 02:58:11 pm »
Code: QB64: [Select]
  1. Dim MyArr(1 to n)

You can DIM an array to any lower to upper index.

Code: QB64: [Select]
  1. DIM MyArr(-10 to 5)
« Last Edit: May 21, 2019, 03:00:09 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Option Base
« Reply #2 on: May 21, 2019, 03:44:05 pm »
Hi bplus . If you run the following code, there is no error, Text( 1 to 10) does in fact have a Text(0) = 0.

DIM Test(1 TO 10)
FOR x = 1 TO 10
    Text(x) = x + 1
NEXT x
PRINT
PRINT "The value of Text(0) is "; Text(0)


If I code OPTION BASE 1 before that Dim line I do get an error.
 
In the 2nd example (Dim Test2(-10 to 5), here again Test2(0) will have a value no matter if  OPTION BASE 1 is stated or not.
 So what I'm looking to do would be something like this


DIM Test2(-10 TO 5)
OPTION BASE 1
DIM Test(1 TO 10)


Where the OPTION BASE 1 would apply only to the Array which would not have a (0). This small example would suggest that all arrays Dimensioned prior to the OPTION BASE statement would have a (0) whereas all arrays Dimensioned after the OPTION BASE 1 statement would not have a (0). I have oh so many Dimensioned Arrays in my program. I never thought about partitioning them into BEFORE and AFTER an Option statement.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Option Base
« Reply #3 on: May 21, 2019, 03:54:43 pm »
Hi bplus . If you run the following code, there is no error, Text( 1 to 10) does in fact have a Text(0) = 0.

DIM Test(1 TO 10)
FOR x = 1 TO 10
    Text(x) = x + 1
NEXT x
PRINT
PRINT "The value of Text(0) is "; Text(0)


If I code OPTION BASE 1 before that Dim line I do get an error.
 
In the 2nd example (Dim Test2(-10 to 5), here again Test2(0) will have a value no matter if  OPTION BASE 1 is stated or not.
 So what I'm looking to do would be something like this


DIM Test2(-10 TO 5)
OPTION BASE 1
DIM Test(1 TO 10)


Where the OPTION BASE 1 would apply only to the Array which would not have a (0). This small example would suggest that all arrays Dimensioned prior to the OPTION BASE statement would have a (0) whereas all arrays Dimensioned after the OPTION BASE 1 statement would not have a (0). I have oh so many Dimensioned Arrays in my program. I never thought about partitioning them into BEFORE and AFTER an Option statement.

There is a difference between words Test and Text. I do that all the time too! :)

Also arrays up to length 10 don't need to be DIM.

  [ You are not allowed to view this attachment ]  
« Last Edit: May 21, 2019, 03:58:37 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Option Base
« Reply #4 on: May 21, 2019, 04:40:35 pm »
Ya, I caught that typo right after I posted. Dim MyArr (1 to 100) does eliminate the (0) index. So far it also appears I can place the OPTION BASE 1 statement just before all those arrays where I want the (0) index to be gone. All arrays declared before the OPTION BASE 1 do keep the (0) index. Here's hoping this change not only doesn't crash the program but is in fact what I'm looking for ... oh those damn unintended consequences.  I appreciate the help bplus, thanks.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Option Base
« Reply #5 on: May 21, 2019, 04:45:35 pm »
Yeah, next time pay attention to your typing instead of staring at Eliza's TTS.

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

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Option Base
« Reply #6 on: May 22, 2019, 04:42:17 pm »
{imagines someone asking Eliza are you married? What are you doing tonight? Would you like to go out for drinks? come back to my place?}
QB64 is the best!

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Option Base
« Reply #7 on: May 22, 2019, 08:00:59 pm »
What's this Eliza you speak of?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Option Base
« Reply #8 on: May 22, 2019, 10:40:03 pm »
What's this Eliza you speak of?

https://www.qb64.org/forum/index.php?topic=88.msg423#msg423

https://en.wikipedia.org/wiki/ELIZA

And latest reincarnation here:
https://www.qb64.org/forum/index.php?topic=1361.0


On the subject of OPTION BASE, I think it is only needed in QB64 to keep compatible to old QB4.5 code as you can now set the lower bound of an array to any integer you need. I wouldn't use it for any new code, don't see any advantage.
« Last Edit: May 22, 2019, 10:54:47 pm by bplus »

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Option Base
« Reply #9 on: May 23, 2019, 03:18:40 pm »
Ah, thanks.

I used Eliza as a kid, was pretty fun at the time.  Later, on a 286 IBM PC, I found a program called "Babble" that was really amazing.  It learns word patterns as it goes, I believe it uses Markov chains, and over time improves its responses.  It also had a lot of speech filters, such as "Pig Latin", "Stutter", "Elmer Fudd", etc.  Fun times for a kid!
« Last Edit: May 23, 2019, 03:20:00 pm by Raven_Singularity »