Page 1 of 1

Variable grabbing

Posted: Thu Oct 27, 2005 5:46 pm
by Patz QuickBASIC Creations
Is there a way in QBasic that if I put a prompt in for a variable name, it will make the variable appear?

Psudeo code:

Code: Select all

INPUT "Variable Name: ", Name$
Let Variable = VariableName(Name$) ' this is the psudeo code (ex a function)
Print Variable
[/code]

Posted: Thu Oct 27, 2005 6:55 pm
by {Nathan}
I do not think this is possible, but if you can tell us what you are using this for we could always try to think of a backup plan... :wink:

Posted: Thu Oct 27, 2005 8:04 pm
by Rattrapmax6
I really don't get what you mean.... This?

Code: Select all

DECLARE FUNCTION ReturnVarName(Var AS STRING) AS STRING

INPUT "Variable Name: ", Namer$
Variable$ = ReturnVarName(Namer$)

PRINT Variable$
SLEEP


FUNCTION ReturnVarName(Var AS STRING) AS STRING
    ReturnVarName = Var
END FUNCTION
:roll:

Re: Variable grabbing

Posted: Thu Oct 27, 2005 8:05 pm
by moneo
PQBC wrote:Is there a way in QBasic that if I put a prompt in for a variable name, it will make the variable appear?

Psudeo code:

Code: Select all

INPUT "Variable Name: ", Name$
Let Variable = VariableName(Name$) ' this is the psudeo code (ex a function)
Print Variable
[/code]
This almost sounds like a debugging function within a program where you can monitor the value of a given variable by issuing its name.

Anyway, what you could do is make a list of all the variables that you want this routine to print the corresponding value for.

Then assign an external name for each of these variables. The external name is the name that the user will type in. The external name doesn't have to match the actual internal variable name.

Now, set up a SELECT CASE something like this:

Code: Select all

INPUT "Variable Name: ", VName$
SELECT CASE VName$
            CASE "VarA": print A
            CASE "VarB": print B
            CASE "VarC": print C
            CASE ELSE
                 print "Variable name unknown"
END SELECT
In above example, VarA is the external name of the variable, and the actual internal name is A.
*****

Posted: Fri Oct 28, 2005 4:07 am
by matt2jones
Do you mean:

code:

Code: Select all

A=100
input  A

Output:

>100_


...

Eh, as in the value of the variable appears at the cursor when you call the input function?

I think older versions of basic used to do that...

matt

Posted: Fri Oct 28, 2005 7:55 am
by {Nathan}
actually, he wants something like this kinda...

Code: Select all

INPUT "Varible name?", hi$
(the string in hi$ would be the name of this varible) = blah blah blah
Like that, but I do not think that is possible.

Posted: Fri Oct 28, 2005 11:27 am
by Kyle
What's wrong with Rattrap's method?

EDIT: Nevermind, I didn't look at it properly. Rattrap's code doesn't work :lol:

Posted: Fri Oct 28, 2005 12:09 pm
by Patz QuickBASIC Creations
Ok, not explained well enough.

I think moneo's got the idea. Get the variable name and return that variable's value. EX.
I want the screen to have wrote:Variable name? RED <RED as an example
The variable RED has a value of 555. (after this is totally optional) Change it? (y/n) y
To what value? 147
I don't want to have a huge chain of SELECT CASEs in this sub either. It would be a waste of space, since ECLIPSE doens't need it anyway.

Posted: Fri Oct 28, 2005 2:09 pm
by {Nathan}
Ahh, I see... I do not think that is possible unless you used A LOT of constants and a lookup table...

Wait! I have an idea you can tool around with! You will need to have all your varibles set global by COMMON though, not DIM SHARED.

PSUEDO CODE:

Code: Select all

INPUT "varible:", varible
OPEN "code.bas" FOR OUTPUT AS #1
Writethisstring = "hivar" + " = " + Varible"
Writethisalsok = "OPEN "var.txt" FOR INPUT AS #1: WRITE #1, hivar: CLOSE #1" 
WRITE #1, Writethisstring
WRITE #1, Writethisalsok
CLOSE #1
SHELL "QBASIC.EXE /run code.bas"
OPEN "var.txt" FOR INPUT AS #1: INPUT #1, varvalue
I just realized thats completely wrong... so I stopped, but still, its interesting...

Posted: Fri Oct 28, 2005 3:52 pm
by The Awakened
Heh, this was a question I've been on the verge of asking for a few years, but I found a way around having to use it.

Basically, what I wanted to use it for was to change variables from scripts. Example script file:

Code: Select all


changevariable playerX, 2
changevariable money, 40000

A giant lookup table would work, as Nathan said. You'd have to have a huge array of pointers to each of the variables. Example FB code:

Code: Select all


dim shared vars(100) as integer ptr

vars(0) = @playerx
vars(1) = @playery
vars(2) = @playerHP

'etc

Then in the script, to access the variable, I'd have to refer to it by its index. For users typing in the variable themselves, I can't think of anything offhand.

If you're using QB then that code won't work, you'll have to mess with VARSEG and VARPTR. Also, for different variable types, you'll have to make different arrays, one for integers, one for floating point, one for strings, etc.

Good luck though. :)

Posted: Fri Oct 28, 2005 7:45 pm
by moneo
PQBC wrote:.....
I don't want to have a huge chain of SELECT CASEs in this sub either......
Ok, let's put this problem into perspective. How many variables are you talking about, 20 or 30? If you're talking about hundreds, then maybe we need to think about a more sophisticated approach.

For 20 or 30, you're going to either use 20-30 CASE statements (like I suggested), or some other similar code that needs to repeat 20-30 times. So what's the big deal?

There's no free lunch. :wink:
*****

Posted: Sat Oct 29, 2005 6:18 am
by Z!re
Function pointers..

You can have the function name DYLIBLOAD under FB, function name can be dynamic.

Untested, there are probably errors:

Code: Select all

wanted_function$ = "Getval_"+str(someNumber)
libhandle = DYLIBLOAD "myDLLthing)
The_function = DYLIBSYMBOL(libhandle, wanted_function$)
To gain more speed you should only DYLIBLOAD once at the beginning of your program..

This requires you make a function for each variable you wish to modify.. but it's fast..