Raised This Month: $51 Target: $400
 12% 

[SOLVED] KeyValue Parsing


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-23-2015 , 22:27   [SOLVED] KeyValue Parsing
Reply With Quote #1

This is a huge pain in the ass to do, sometimes getting to the point where I just write something random and it works, and I write something that SHOULD work and it does absolutely nothing.

Relevant code:

PHP Code:
    do
    {
        
// Grab the ID
        
decl String:AttributeID[6];
        
KvGetSectionName(handleAttributeID6);
        
        
// Grab its value
        
decl String:AttributeValue[20];
        
KvGetString(handleAttributeIDAttributeValue20);
        
        
ServerCommand("say [%s, %s]"AttributeIDAttributeValue);
        
    } while (
KvGotoNextKey(kvfalse)); 
Relevant section of the file:

PHP Code:
        "Weapons"
        
{
            
"0"
            
{
                
"81"    "2.0"
                "25"    "3.0"
            
}
        } 
Server prints out:

PHP Code:
[81, ]
[
25, ] 
I want it to print out:

PHP Code:
[812.0]
[
253.0
Any reasons why it's not grabbing the decimal values from the file? Does the KV stack have to point at the "0" node before I use the "KvGetString" native, and do I have to have all the known keys (81, 25) prior to grabbing the decimal values? (Which would require 2 iterations on the section - 1 to grab the keys and then 1 to grab the values - and that sounds like a performance sink.)

You may NOT assume that I know what the keys & values of the "0" block is. (That is, the keys can vary to be 22, 28, 45 or 1, 2, 3, 4 or any size + combo of numbers.)

Thanks!

============================================= ==

Problem has been solved: https://forums.alliedmods.net/showpo...32&postcount=6

Last edited by Potato Uno; 01-24-2015 at 17:22. Reason: Problem solved (better)
Potato Uno is offline
ezetna
Junior Member
Join Date: Oct 2012
Old 01-24-2015 , 02:08   Re: KeyValue Parsing
Reply With Quote #2

use
native Float:KvGetFloat(Handle:kv, const String:key[], Float:defvalue=0.0);
ezetna is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-24-2015 , 02:14   Re: KeyValue Parsing
Reply With Quote #3

Tried that, returns 0.0 and 0.0 both (which is the same as the string case).

I'm probably going to try the 2 iteration trick tomorrow and see if it works.
Potato Uno is offline
wanted2411
Senior Member
Join Date: Jun 2012
Old 01-24-2015 , 03:26   Re: KeyValue Parsing
Reply With Quote #4

Did you jump at Section? If yes, then ofc you'll not get valid.

>>

"81" // is the section
{

}


You need to jump to section "0" and then get keys "81" and "25" by using 'KvGetFloat()'.

Last edited by wanted2411; 01-24-2015 at 03:28.
wanted2411 is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-24-2015 , 12:35   Re: KeyValue Parsing
Reply With Quote #5

Quote:
Originally Posted by wanted2411 View Post
Did you jump at Section? If yes, then ofc you'll not get valid.

You need to jump to section "0" and then get keys "81" and "25" by using 'KvGetFloat()'.
That's what I ended up having to do. The problem with that is I had to loop across the inner section twice - once to get the keys and once to get the values. I was hoping that once I got the key I could also get the value simultaneously, but it doesn't seem like that's how the keyvalue API is built.

Problem has been solved. Thanks for your help!
Potato Uno is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 01-24-2015 , 15:38   Re: [SOLVED] KeyValue Parsing
Reply With Quote #6

Quote:
Originally Posted by Potato Uno View Post
This is a huge pain in the ass to do, sometimes getting to the point where I just write something random and it works, and I write something that SHOULD work and it does absolutely nothing.

Relevant code:

PHP Code:
    do
    {
        
// Grab the ID
        
decl String:AttributeID[6];
        
KvGetSectionName(handleAttributeID6);
        
        
// Grab its value
        
decl String:AttributeValue[20];
        
KvGetString(handleAttributeIDAttributeValue20);
        
        
ServerCommand("say [%s, %s]"AttributeIDAttributeValue);
        
    } while (
KvGotoNextKey(kvfalse)); 
Relevant section of the file:

PHP Code:
        "Weapons"
        
{
            
"0"
            
{
                
"81"    "2.0"
                "25"    "3.0"
            
}
        } 
Server prints out:

PHP Code:
[81, ]
[
25, ] 
I want it to print out:

PHP Code:
[812.0]
[
253.0
Any reasons why it's not grabbing the decimal values from the file? Does the KV stack have to point at the "0" node before I use the "KvGetString" native, and do I have to have all the known keys (81, 25) prior to grabbing the decimal values? (Which would require 2 iterations on the section - 1 to grab the keys and then 1 to grab the values - and that sounds like a performance sink.)

You may NOT assume that I know what the keys & values of the "0" block is. (That is, the keys can vary to be 22, 28, 45 or 1, 2, 3, 4 or any size + combo of numbers.)

Thanks!

============================================= ==

Problem has been solved. The bolded section of that question I asked is the answer to the problem (have to get the keys once, then go back up a node, then get the values). This is, of course, assuming the keys inside the section are not known. (If the keys are known, then just directly get the values.)
this should work:
PHP Code:
    do
    {
        
// Grab the ID
        
decl String:AttributeID[6];
        
KvGetSectionName(handleAttributeID6);
        
        
// Grab its value
        
decl String:AttributeValue[20];
        
KvGetString(handleNULL_STRINGAttributeValue20); //Docs say: Name of the key, or NULL_STRING.
        
        
ServerCommand("say [%s, %s]"AttributeIDAttributeValue);
        
    } while (
KvGotoNextKey(kvfalse)); 
__________________

Last edited by WildCard65; 01-24-2015 at 15:39.
WildCard65 is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-24-2015 , 17:21   Re: [SOLVED] KeyValue Parsing
Reply With Quote #7

Quote:
Originally Posted by WildCard65 View Post
this should work:
PHP Code:
    do
    {
        
// Grab the ID
        
decl String:AttributeID[6];
        
KvGetSectionName(handleAttributeID6);
        
        
// Grab its value
        
decl String:AttributeValue[20];
        
KvGetString(handleNULL_STRINGAttributeValue20); //Docs say: Name of the key, or NULL_STRING.
        
        
ServerCommand("say [%s, %s]"AttributeIDAttributeValue);
        
    } while (
KvGotoNextKey(kvfalse)); 
Oh beautiful that works perfectly! Thank you so much! That should cut processing time by about a half.
Potato Uno is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:25.


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