Page 1 of 1

Help ! (table)

Posted: Thu May 25, 2006 5:00 pm
by pfoo
Hello !

I'm starting writing a little chat server, but i've got some problem..

Code: Select all

Structure base
  Login.s
  Pass.s
EndStructure
Dim Database.base(1000)

Database(pfoo)\login = "pfoo"
Database(ukw)\login = "ukw"
My first problem was here:
database(pfoo)\login = "pfoo"
database(ukw)\login = "pfoo"
Apparently, you can't use a string in a table ... so, when i:
Debug Database(pfoo)\login it returns "pfoo"
Debug Database(ukw)\login it returns "pfoo"
Purebasic return "pfoo" everytime...

I fixed this :

Code: Select all

Structure base
  Login.s
  Pass.s
EndStructure
Dim Database.base(1000)

Global pfoo.w
Global ukw.w
pfoo = 0
ukw = 1

Database(0)\login = "pfoo"
Database(1)\login = "ukw"
So, when I
Debug Database(pfoo)\login it return "pfoo"
Debug Database(ukw)\login it return "ukw"

But, when a user connect to the server, he has to log-in via the LOGIN <username> command.
when i type "LOGIN(chr10)pfoo(chr13):

login$ = StringField(StringField(PeekS(buffer), 2, Chr(10)), 1, Chr(13))
login$ contain "pfoo"


after, i need to check if the login exist in the database:
If (Database(login)\login = login$)

Here is the problem. I need to check in "database(pfoo)" which will call "database(0), but purebasic check "database(login)" ...
In TCL, this could be arranged with database([set login]) ...
Is there the same command in purebasic ?

Thx !

Posted: Thu May 25, 2006 5:17 pm
by Pupil
You're doing things the wrong way, PB doesn't work the way you seem to assume.. PB doesn't use associative arrays, if that is what you think?

You will need to keep track of the number of users in a variable(at least i would).
To find a user you have to iterate through the array until you find the one that matches, i.e. something like this would do:

Code: Select all

; Find user that is login in
For i = 0 to NrOfUsers-1
  If Database(i)\login = login$
    ; Found a matching login! Do what's
    ; necessary i.e. store the user id ('i') in a variable and
    ; break out of the loop or whatever
  Endif
Next i