Variable Problem

Just starting out? Need help? Post your questions and find answers here.
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Variable Problem

Post 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"?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Variable Problem

Post 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$
SPAMINATOR NR.1
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post 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:
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post 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 ?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post 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
SPAMINATOR NR.1
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post 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.
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post 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.
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post 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.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post 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
SPAMINATOR NR.1
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post 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!
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post 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...
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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?
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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 ;)
--Kale

Image
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
Summers
User
User
Posts: 15
Joined: Tue Sep 30, 2003 3:20 pm

Post 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
Post Reply