Page 1 of 2

Variable Problem

Posted: Wed Oct 01, 2003 3:04 pm
by Summers
I'm sorry, I know this must be clearly obvious but I'm really not seeing it at the moment. I have the following test code and this is the only thing I seem to be tripping up on:
entry$="Joanne"
For i = 1 To 5
entry$.b = entry$.b + 1
Debug entry$
Debug entry$.b
Next

entry$="Peter"
For i = 1 To 5
entry$.b = entry$.b + 1
Debug entry$
Debug entry$.b
Next

If you run that you'll see the name and variable count. Now, why is Peter continuing Joanne's count? Isn't this supposed to create a new variable called "entry$.b" - which is really "peter.b"?

Re: Variable Problem

Posted: Wed Oct 01, 2003 3:11 pm
by Rings
entry$ and entry$.b are totaly different variables. the first is a string
(i prefer suffix .s for string instead of $ ), the other is a byte-variable with the name entry$

Posted: Wed Oct 01, 2003 3:12 pm
by dmoc
Hmmm, you need to read-up on variable naming. "entry$" would be a string but "entry$.b" should make PB be very sick :twisted:

Posted: Wed Oct 01, 2003 3:17 pm
by Summers
I'm sorry, I'm just not following. If I do:

entry$="joe"

then

entry$.b

Then the variable is automatically named joe.b ?

Posted: Wed Oct 01, 2003 3:38 pm
by Rings
Summers wrote:I'm sorry, I'm just not following. If I do:

entry$="joe"

then

entry$.b

Then the variable is automatically named joe.b ?
entry$ and entry$.b are totaly different variables. the first is a string
, the other is a byte-variable with the name entry$ It have nothing to do with a Bytefield (Charfield) like in C-languages

Posted: Wed Oct 01, 2003 4:13 pm
by Summers
So if I do:

entry$="Joanne"

Then how do I get a variable to automatically name itself joanne.b, using the above string variable..? The value of entry$ is going to change several times in one routine.

Posted: Wed Oct 01, 2003 4:30 pm
by dmoc
Have you been doing PHP/Python/Etc lately? PB (and most or BASIC's) is not not like these scripting languages. As powerful as they are they are no way as quick as PB. One of the things you cannot do is declare variables dynamically in this fashion. So if you want "joanne.b" you have to declare it as such.

Posted: Wed Oct 01, 2003 5:02 pm
by Summers
Say I have var1.s with contents of "peter". I want to create a second variable called "peter.b" using the contents of var1.s. I am reading everyone's replies, but no-one seems to have answered this, or any possible workaround.

Posted: Wed Oct 01, 2003 7:19 pm
by Rings
Summers wrote:Say I have var1.s with contents of "peter". I want to create a second variable called "peter.b" using the contents of var1.s. I am reading everyone's replies, but no-one seems to have answered this, or any possible workaround.
var1.s="Peter"
Peter.s=var1
debug var1
debug peter

Posted: Wed Oct 01, 2003 7:30 pm
by Summers
var1.s="Peter"
Peter.s=var1
debug var1
debug peter
No no, because I have to tell it to name the variable 'Peter' (line 2) - it has to do it automatically!

Posted: Wed Oct 01, 2003 7:34 pm
by Blade
Summers wrote:Say I have var1.s with contents of "peter". I want to create a second variable called "peter.b" using the contents of var1.s. I am reading everyone's replies, but no-one seems to have answered this, or any possible workaround.
dmoc was very clear:

You can't!

I suggest to find another solution using arrays and/or linked lists...

Posted: Wed Oct 01, 2003 7:38 pm
by Pupil
What you're asking for is simply not possible in PureBasic, the compiler doesn't support this kind of variable definition. That's why you're not getting any answers to your problem. As far as i know there's no easy trick to work around this, you have to create some sort of framework for this yourself at the moment if you really want this feature... Maybe you could make some sort of associative array to simulate this behaviour?

Posted: Wed Oct 01, 2003 7:58 pm
by Kale
I think i know what you are asking for, an Eval() command to evaluate pieces of code from strings during runtime.

such as:

Code: Select all

For x = 1 To 100
    Eval("Var" + Str(x) + ".s = " + Chr(34) + "String" + Str(x) + Chr(34))
Next x
then you could use:

Code: Select all

For x = 1 To 100
    Eval("Debug Var" + Str(x))
Next x
To display:

Code: Select all

String1
String2
String3
...
This is not possible because, PureBasic is a statically typed language, There is no Eval() command. So i'm afraid this can not be achieved.

I think you have been too spoiled by interpreted, dynamically typed scripting languages for too long ;)

Posted: Wed Oct 01, 2003 8:58 pm
by GedB
Summers,

Just to reiterate whats been said before:

There are two main types of language: Compiled and Interpreted.

PHP, Python, Perl, Ruby. These are all interpreted. Iterpreted languages are slower, but very expressive. You can do thing like create dynamic variables, which is what you are doing.

PureBasic, like C++ or Delphi, is Compiled. The source code is compiled into a binary executable consisting of machine code instructions. These are instructions that can be directly executed by the processor, so they run much faster.

Another point is that in PB a variables type is declared by appending a type identifier to the end of the variables first mention.

var1.b is equivalent to char var1 in c or java.

This is a very efficient way of doing it, much less typing. However, it is a little confusing if your used to the dot being used for objects members.

Posted: Thu Oct 02, 2003 5:00 pm
by Summers
Thanks for the replies, peoples and for having patience to deal with my daft questions :D I'm now studying arrays and will go that way - once I figure it all out :o