AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [help] cellarray.inc (https://forums.alliedmods.net/showthread.php?t=143156)

Lightokun 11-15-2010 08:57

[help] cellarray.inc
 
Hello again.
Now i need help with cellarray.inc. :crab:

I want to store data (like id, key=> value) in array and easy access to it.
But i wasted a lot of time trying to do it myself.

I need to create array for each player like this to retrieve data ("key" and "value")
Code:

player_id => array(
        string_key => string_value,
        another_string_key => another_string_value
),
another_player_id =>
        string_key => string_value,
        another_string_key => another_string_value

So, anybody can explain or write a code how to create dynamic array with 32 cells which contains data like "key"=>"value" n times?

Basically i want to store for each player id their settings on connect in array like: nvg color, language, hat and etc. data which is loading from DB and i don't want to create a lot of arrays for this (data can be dynamic, like some player have "nvg" key, another don't)

Exolent[jNr] 11-15-2010 12:50

Re: [help] cellarray.inc
 
You need to look at celltrie.inc for this.
CellArrays only use integer keys like normal arrays, but CellTries use string keys.
The problem with CellTries is that you can't iterate through all of the keys, so a combination of both would be needed.

Arkshine 11-15-2010 13:15

Re: [help] cellarray.inc
 
Quote:

The problem with CellTries is that you can't iterate through all of the keys, so a combination of both would be needed.
CellTravTrie exists for that.

fysiks 11-15-2010 19:21

Re: [help] cellarray.inc
 
It looks like, to me, that he needs an array of Tries (one Trie for each player).

Depending on your actual implementation it's possible that you could just use an Enum/data structure and an array (probably best if your list of keys is not too many).

Something like this:

PHP Code:

enum KEYS {
    
KEY1[32],
    
KEY2[32]
}

new 
my_array[33][KEYS]

client_print(idprint_chatmy_array[id][KEY1]) 

Correct me if I'm wrong on the data structure.

If he uses Tries I don't think he would need to traverse it from his explaination.

Lightokun 11-16-2010 00:44

Re: [help] cellarray.inc
 
Ok, i'll try both methods later and post the result. Thanks for help. :)

Lightokun 11-16-2010 03:32

Re: [help] cellarray.inc
 
Solved!
Thanks to all.

Some code example:
Code:

public parse_cfg(id,settings[]) {
    ...
    for(i=0; i<=max; i++) {
        re = regex_match(tmp[i], SETTINGS_PATTERN, num, error, 127)
        if (re >= REGEX_OK) {
            regex_substr(re, 1, str2, 63)
            regex_substr(re, 2, str3, 63)
            regex_free(re)
           
            TravTrieSetString(PlayerData, str2, str3)
        }
    }
   
    TravTrieSetCellEx(PlayerArray, id, PlayerData)
}

public ep_get_settings_param(id, settings_name[], search[]) {   
    new TravTrie:array
    static value[64]
    TravTrieGetCellEx(PlayerArray,id,array)
    TravTrieGetString(array,search,value,63)
    log_amx("value: %s",value)
}



All times are GMT -4. The time now is 11:21.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.