Page 1 of 1

Variable variables

Posted: Sun Jul 02, 2006 12:48 pm
by fenyctis
Heh, like the topic title.

So what I'm looking for is how to have variable variables:

Code: Select all

var{variable} = test
As in:
var987 = test1
var134 = test2
As in: creating variables during the programme execution with variable names.

I realize this might be a little unclear..

~fenyctis

Posted: Sun Jul 02, 2006 12:54 pm
by srod
No chance! :?

Once a program is compiled, all variable names lose their meaning and translate to locations within the computers memory (the heap, the stack etc.)

The closest you'll get is to use an array or a linked list or perhaps even allocate some memory etc.

Of course, you could probably hack something together at the preprocessor stage, perhaps with macros etc.

Posted: Sun Jul 02, 2006 12:56 pm
by SunSatION
I think you should us arrays in this case.

Code: Select all

Dim var(1000)
varentry = 987
var(varentry) = test1

;Here is to show you that you can change the variable
varentry = 987-853 ; which is equal to 134
var(varentry) = test2
I also think that variable variables are not possible to create

Posted: Sun Jul 02, 2006 12:58 pm
by fenyctis
srod wrote:No chance! :?

Once a program is compiled, all variable names lose their meaning and translate to locations within the computers memory (the heap, the stack etc.)

The closest you'll get is to use an array or a linked list or perhaps even allocate some memory etc.

Of course, you could probably hack something together at the preprocessor stage, perhaps with macros etc.
I remember mIRC, in which you could create such variables:

Code: Select all

%var $+ 576 = test
would result in

Code: Select all

%var576 = test

Posted: Sun Jul 02, 2006 1:00 pm
by fenyctis
SunSatION wrote:I think you should us arrays in this case.

Code: Select all

Dim var(1000)
varentry = 987
var(varentry) = test1

;Here is to show you that you can change the variable
varentry = 987-853 ; which is equal to 134
var(varentry) = test2
I also think that variable variables are not possible to create
The numbers are actually ClientIDs of connecting clients and I want to store some information they pass on (username, hostname etc.) Maybe you've got a good idea?

Posted: Sun Jul 02, 2006 1:00 pm
by SunSatION
Well, mIRC is not a compiler. That's the only reason why that's allowed. With mIRC scripting, variables are not stored in the data segment, but saved in an ini file :P

In that case, you can use Multidimensional array

Maybe this is not so perfect, but it gives you some hints

Code: Select all

 dim var(5000,5000,5000,5000)


such that you can save 4 parameters for the given clientID

I noticed that ClientIDs are given in multiples of 4(at least on v3.94). So, if you wish to save space, just divide them by 4(that's what I did).

Posted: Sun Jul 02, 2006 1:01 pm
by fenyctis
SunSatION wrote:Well, mIRC is not a compiler. That's the only reason why that's allowed. With mIRC scripting, variables are not stored in the data segment, but saved in an ini file :P
XD Good one.

Posted: Sun Jul 02, 2006 1:03 pm
by srod
Yes, but unless I'm mistaken, mIRC is not a compiler.

The only way you'll be able to implement something along these lines, is before compilation, e.g. with a macro.

**EDIT - too slow! :D

Posted: Sun Jul 02, 2006 1:05 pm
by fenyctis
srod wrote:Yes, but unless I'm mistaken, mIRC is not a compiler.

The only way you'll be able to implement something along these lines, is before compilation, e.g. with a macro.

**EDIT - too slow! :D
The numbers are actually ClientIDs of connecting clients and I want to store some information they pass on (username, hostname etc.) Maybe you've got a good idea? I'm afraid of databases, because they require third-party software other people may not have. Having INI files storing these variables is unsafe and requires a lot of writing to the harddrive if my server is busy.

Posted: Sun Jul 02, 2006 1:12 pm
by SunSatION
Or else, if you were in mIRC scripting and don't want to use Multidimensional array, make something like $token in mIRC with PureBasic.

Posted: Sun Jul 02, 2006 1:16 pm
by srod
Assuming you're writing all this in Purebasic, then I wouldn't worry about databases. If I've understood correctly, then you could use SQLite easily enough - providing the server application is accessing it as if it were a stand alone app. SQLite simply requires that you copy a single .dll file onto the machine running the application; no activex controls, no MSDAC components, no fiddly ODBC setups.

However, providing the number of clients is not huge, why not use an array of structures:

Code: Select all

Structure ClientInfo
  ClientId.l
  Username.s
  Hostname.s
EndStructure

Dim clients.ClientInfo(999) ;Enough storage for 1000 clients.

clients(0)\Username="Bob"
You could even use a linked list instead.

Writing such an array out to an SQLite database is simplicity itself.

Posted: Sun Jul 02, 2006 1:18 pm
by fenyctis
srod wrote:Assuming you're writing all this in Purebasic, then I wouldn't worry about databases. If I've understood correctly, then you could use SQLite easily enough - providing the server application is accessing it as if it were a stand alone app. SQLite simply requires that you copy a single .dll file onto the machine running the application; no activex controls, no MSDAC components, no fiddly ODBC setups.

However, providing the number of clients is not huge, why not use an array of structures:

Code: Select all

Structure ClientInfo
  ClientId.l
  Username.s
  Hostname.s
EndStructure

Dim clients.ClientInfo(999) ;Enough storage for 1000 clients.

clients(0)\Username="Bob"
You could even use a linked list instead.

Writing such an array out to an SQLite database is simplicity itself.
Doesn't that array require a lot of memory?

Posted: Sun Jul 02, 2006 1:20 pm
by srod
How many clients are we talking about?

Posted: Sun Jul 02, 2006 1:28 pm
by fenyctis
srod wrote:How many clients are we talking about?
Never mind, even a thousand clients doesn't consume that much memory.
Say the strings are a max of 30: ((4+30+30)*1000)/1024 = 62kB

Thanks!