Help ! (table)

Just starting out? Need help? Post your questions and find answers here.
pfoo
New User
New User
Posts: 7
Joined: Thu May 25, 2006 3:00 pm

Help ! (table)

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

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