Author Topic: Re: question about using select case functions instead of arrays  (Read 966 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: question about using select case functions instead of arrays
« on: November 26, 2019, 10:30:23 am »
If I am fathoming what you want, all I can say is that it is a bad idea. We may be able to write code of practically unlimited size but that doesn't mean we can compile code of unlimited size, there is a limit on that side of it. Your not the first that has had the,quite honestly, irrational idea of hard coding something like this out. It never ends good, and you will find your the only one here now. Efficiency is a good thing to learn.And arrays are very efficient if you use them correctly.

I would strongly suggest you take another look and attempt to do what you need with arrays before spending a lot of time and effort to go the other way only to find it will not compile in the end.
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: question about using select case functions instead of arrays
« Reply #1 on: November 26, 2019, 10:44:32 am »
Hello. Your question is detailed, my knowledge of English is crappy :) Perhaps I will answer something you do not ask. I read the questions 3 times, perhaps I understood it good :). Certainly you can insert a function as a parameter of another function. It is commonly used. If I understand well, do you want to put your opponent's answers in the array and write a function that finds a suitable answer in the box?

This is VERY FAST code example, its very stupid, because first computer words database contains 4 words. Computer add players answer to array and generate replyes from it.

Code: QB64: [Select]
  1. Words(0) = "Hi. Nice!"
  2. Words(1) = "My name is computer"
  3. Words(2) = "I am hungry"
  4. Words(3) = "Like it"
  5.  
  6.  
  7. T$ = "Hi. How are you"
  8. DO UNTIL in$ = "END"
  9.     PRINT T$;: INPUT in$
  10.     T$ = Answer$(in$)
  11.     Add in$
  12.  
  13. FUNCTION Answer$ (Player_say$)
  14.     DO UNTIL INSTR(1, Words(search), Player_say$)
  15.         search = search + 1
  16.         IF search > UBOUND(words) THEN Answer$ = "I do not understand": EXIT FUNCTION
  17.     LOOP
  18.     Answer$ = Words(search)
  19.  
  20. SUB Add (TextInput AS STRING)
  21.     i = UBOUND(words)
  22.     REDIM _PRESERVE Words(i + 1) AS STRING
  23.     Words(i + 1) = TextInput
  24.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #2 on: November 26, 2019, 11:14:22 am »
The advantage of using dynamic arrays (REDIM) is that they can be resized as needed where static DIM arrays can not be. Perfect for reading a script into a program that "plays" any amount of scripts like Player did (not just Eliza's). Use any editor to write the script, you don't have to worry about writing indexes for the function PLUS edit the script all you want, no sweat for Player app.

The advantage of using Functions instead of arrays is:
1. You never get an error if you request an "out of bounds" item, you just get nothing "" for string arrays.
2. For unchanging strings you can "LOAD" your data without loading anything at start of program, Beautiful!
Specially for someone using a heck of allot of CONSTANT strings PLUS you don't have an extra file you have to manage in a zip package if you want to share your program (which chatbot or Player app required a script for keywords). PLUS if you want to add to the String constants just add another line to your "FUNCTION".
3. Not recommended for strings you may want to change, because you can't, whereas with a normal array you can and that is why we love them so ;D

I see by Ron's question he is missing the UBOUND function that arrays have, so here is a suggestion for Array Functions, make item (0) the count of the strings the function contains, then to get the UBOUND of the function it is
nItemsOfMyArrayFunction = VAL(myArrayFunction$(0))
always number the strings 1 to N and put N into myFunction$(0)


« Last Edit: November 26, 2019, 11:30:21 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #3 on: November 26, 2019, 11:31:38 am »
Hi Ron,

I added to my reply to address a question about getting an item count out of one of these "array" Functions.

Reread the previous post.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #4 on: November 26, 2019, 11:35:44 am »
The advantage of using dynamic arrays (REDIM) is that they can be resized as needed where static DIM arrays can not be. Perfect for reading a script into a program that "plays" any amount of scripts like Player did (not just Eliza's). Use any editor to write the script, you don't have to worry about writing indexes for the function PLUS edit the script all you want, no sweat for Player app.

The advantage of using Functions instead of arrays is:
1. You never get an error if you request an "out of bounds" item, you just get nothing "" for string arrays.
2. For unchanging strings you can "LOAD" your data without loading anything at start of program, Beautiful!
Specially for someone using a heck of allot of CONSTANT strings PLUS you don't have an extra file you have to manage in a zip package if you want to share your program (which chatbot or Player app required a script for keywords). PLUS if you want to add to the String constants just add another line to your "FUNCTION".
3. Not recommended for strings you may want to change, because you can't, whereas with a normal array you can and that is why we love them so ;D

One more for arrays B+;
#1. Going through the entire array with just a few lines of code:
Code: QB64: [Select]
  1. FUNCTION Check_Item(Item)
  2. For I&=0 to maxArrayBounds
  3.  if item=array(I&) then Result = Runsomething(I&)
  4. Next I&
  5. Check_Item = Result
rather than having thousands to hundreds of thousands of lines of code. to do the same
(FYI Ron: this function runs almost Identically to what you originally posted you wanted to do manually)

Not that having your own style of coding is bad, but I'd much rather you have code that will compile and stay happy rather than getting to a point where you have spent hour upon endless hour coding only to start receiving compilation failure errors and get angry and frustrated.
Granted after becoming radioactive I only have a half-life!

Marked as best answer by ron77 on November 26, 2019, 07:31:56 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #5 on: November 26, 2019, 11:58:29 am »
Opps! too many corrections to Colbalt's code example, forget it!

The point I think is you can use arrays for arguments in QB64 but you can't use Functions as arguments in QB64.

So best way to do huge amount of strings with minimum loading is have a Split helper.
Write the strings in a normal txt editor.
Save the whole file as a string.
Put the string into a function, pop the string into an array with split, use the array as needed, exit function stage right.
« Last Edit: November 26, 2019, 12:20:12 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #6 on: November 26, 2019, 12:22:51 pm »
hi cobalt and bplus...

i tried to follow bplus suggestion about the UBOUND...

bplus is this is what you ment?

not VAL(replay$) but
n = _UBOUND(replay$)

Code: [Select]
FUNCTION Check_Item$(array, Item)  '<<<<<<<<<< add array to parameter list
For I&=lbound(array) to UBOUND(array)
 if item=array(I&) then Check_Item$ = Runsomething(I&) :Exit function  '<<<< no sense sticking around when you have it
Next I&
END FUNCTION

:Exit function  '<<<< no sense sticking around when you have it
Tiss, tiss, tiss...
: I& = UBOUND(array) +1 '<<<< exit the loop and function normally no need for Spaghetti Western code here.

but yeah, that is a more 'fleshed out' version of what I meant.
Granted after becoming radioactive I only have a half-life!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #7 on: November 26, 2019, 01:12:08 pm »
The point I think is you can use arrays for arguments in QB64 but you can't use Functions as arguments in QB64.

That is not entirely correct. I have used a Function as an argument in a SUB call. In fact its part of my Dragon Warrior clone. So I know that can work.

Unless I'm mis-understanding what you mean by that.

Code: QB64: [Select]
  1. print_YESNO YESNO(1)
  2. nul%% = Display_YESNO(YESNO(1))
  3.  
  4. SUB print_YESNO (X1%%)
  5.  PRINT X1%%
  6.  
  7. FUNCTION YESNO%% (id%%)
  8.  YESNO = id%%
  9.  
  10. FUNCTION Display_YESNO%% (id%%)
  11.  PRINT id%%
  12.  Display_YESNO = 0
  13.  
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #8 on: November 26, 2019, 04:35:04 pm »
Quote
Unless I'm mis-understanding what you mean by that.

Yes, mis-undersatnding, sure you can use a value from a function as a string or number argument, no problem!

But you can't take the function itself as an argument like you can with an array.

There is a difference between fx(array(1)) and fx(array), similarly you can do fx(myFunction(10)) but not fx(myFunction), otherwise we could write a general function to search any given array Function.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #9 on: November 26, 2019, 04:53:38 pm »
But you can't take the function itself as an argument like you can with an array.
There is a difference between fx(array(1)) and fx(array), similarly you can do fx(myFunction(10)) but not fx(myFunction), otherwise we could write a general function to search any given array Function.

Can you mock-up an example of that? Cause I'm still not following, sorry B+.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about using select case functions instead of arrays
« Reply #10 on: November 26, 2019, 05:13:42 pm »
Can you mock-up an example of that? Cause I'm still not following, sorry B+.

OK you know the functions SIN, COS, TAN, ...

Impossible Function argument mock up:

Code: QB64: [Select]
  1. Function IDFunction$(Fx as Function)
  2.   Select case Fx
  3.     case SIN : IDFunction$ = "Trig Function"
  4.     case COS: IDFunction$ = "Trig Function"
  5.     case TAN: IDFunction$ = "Trig Function"
  6.     case ABS: IDFunction$ = "Math Function"
  7.     case INSTR: IDFunction$ = "String Function"
  8.     case MID$: IDFunction$ = "String Function"