Variable grabbing

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Variable grabbing

Post 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]
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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:
Image
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post 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:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: Variable grabbing

Post 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.
*****
User avatar
matt2jones
Veteran
Posts: 80
Joined: Sat Feb 19, 2005 8:29 am
Location: elsewhere
Contact:

Post 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
Do not mistake Apathy for feeling Content.

http://www.disjointed.cjb.net - Short Storys
http://matt2jones.deviantart.com - Random Art
http://www.freewebs.com/matt2jones - WebComic
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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.
Image
User avatar
Kyle
Veteran
Posts: 107
Joined: Thu Apr 14, 2005 2:41 pm

Post 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:
Last edited by Kyle on Fri Oct 28, 2005 2:15 pm, edited 1 time in total.
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post 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.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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...
Image
User avatar
The Awakened
Veteran
Posts: 144
Joined: Sun Aug 07, 2005 1:51 am

Post 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. :)
"Sorry for beating you up with a baseball bat Julian, but I DID think that you were a samsquanch."
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post 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:
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post 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..
I have left this dump.
Post Reply