new in pawno, help -
fiha4155 - 16.06.2017
Hello, I am new in pawno coding. My question is how this can happen;
Code:
public OnPlayerText(playerid, text[])
{
print(text[0]);
return 1;
}
it outputs the whole text. not the first character but when I try something like;
Code:
public OnPlayerText(playerid, text[])
{
text[0] = tolower(text[0]);
return 1;
}
it lowers only the first character. it confuses me like really much. as you can see in prints output, text[0] means all of the text but tolower lowers only the first character.
Also, I read tolower function's info on wiki. It says;
Quote:
This function changes a single character to lowercase.
|
So how can it understand I mentioned the first character of that string? text[0] is all of the text, not only the first character. It should give errors in my view.
can somebody help?
Re: new in pawno, help -
Vince - 16.06.2017
This is a bit weird but if you try to
print a string and you specify a start index then the string will be printed
from that start index onward until a null-character is found. If you only want to print a single character you can try something like this:
PHP Code:
printf("%c", text[0])
In all other instances it will indeed return that single character.
Also the language is called
Pawn. Pawno is an editor.
Re: new in pawno, help -
fiha4155 - 17.06.2017
@Vince, how do you know if it represents a starting point or a single character? And also why do we use something like idx in strtok, strrest functions? You create new variable named idx with no value, use it as a &index in those functions. What does & mean before &index? If idx doesn't have a value then why we need to use it? I say it has no value because;
Code:
new cmd[128], idx;
cmd = strtok(cmdtext, idx);
As you can see, after creating it, you set no value to it. You use directly in strtok function.
btw, info of strtok:
Quote:
This (strtok) is used to search a string and find a variable typed after a " " (space), then return it as a string.
|
So if it finds the value after space and returns it, why do you need an additional second argument like idx? Let me explain what I say.
Code:
new
string[128],
splitted[128]
;
string = "Hello world I am trying to understand pawn."
splitted = strtok(string,[IDK WHAT TO PUT HERE, IN MY VIEW YOU DONT NEED TO PUT SOMETHING])
splitted must be "world I am trying to understand pawn." because strtok found the thing after the space and returned it. Am I wrong?
I would be really happy if you answer all of these.
Re: new in pawno, help -
Vince - 17.06.2017
The ampersand (&) means that a variable is passed "by reference" which means the function can alter the original variable. Unlike variables that are passed "by value" (the default) where - in a sense - a copy of the original variable is sent instead. For example:
PHP Code:
main()
{
new byref;
printf("%d", byref); // prints 0
DoSomethingWithByRef(byref);
printf("%d", byref); // prints 5
}
DoSomethingWithByRef(&value)
{
value = 5;
}
In the case of strtok, the idx (index) variable stores the position where a space was encountered. So if strtok is called again with the same idx parameter (which now does have a value) it will start its search from that position onward.
However, strtok is horribly outdated. Don't use it. Use sscanf instead.
Re: new in pawno, help -
DRIFT_HUNTER - 17.06.2017
Quote:
@Vince, how do you know if it represents a starting point or a single character?
|
Since Vince didnt actually answered these (he did but not in a way you expect since it was in his first post). You dont know if its starting point or a single character. You pass starting address, function that formats or prints string just starts from there and goes up until it gets to a null character or hits array size limit (pawn keeps track of these at compilation time).
Re: new in pawno, help -
OneDay - 18.06.2017
PHP Code:
native print(string []);
native tolower(char);
that is how to know - the functions are different parameters
Re: new in pawno, help -
fiha4155 - 18.06.2017
Guys, I thank you very much I get it all now. Thanks especially to Vince. OneDay, thank you for showing up them. DRIFT_HUNTER I understood what you mean too, thanks. Let me ask one more question. How can we compare two strings?
Code:
(string[index] > ' ')
like in strtok function. It is a bit awkward.
Re: new in pawno, help -
Dayrion - 18.06.2017
You can use strcmp (called str(->ing)cmp(->comparision)). Take a look on the wiki for futher details :
https://sampwiki.blast.hk/wiki/Strcmp
A basically example:
PHP Code:
if(strcmp("hey", string) == 0)
...
If you want to compare 2 caracter, which is not the same thing, you need to compare the ASCII code of those 2 characters (All character there:
http://www.ascii-code.com/).
Re: new in pawno, help -
fiha4155 - 18.06.2017
Quote:
Originally Posted by Dayrion
You can use strcmp (called str(->ing)cmp(->comparision)). Take a look on the wiki for futher details : https://sampwiki.blast.hk/wiki/Strcmp
A basically example:
PHP Code:
if(strcmp("hey", string) == 0)
...
If you want to compare 2 caracter, which is not the same thing, you need to compare the ASCII code of those 2 characters (All character there: http://www.ascii-code.com/).
|
I get what you say, but my question was isn't it a bit awkward to say like "a character is bigger than a space". How pawn compare them itself? With comparing the ASCII numbers of them?
Re: new in pawno, help -
DRIFT_HUNTER - 18.06.2017
Quote:
Originally Posted by fiha4155
I get what you say, but my question was isn't it a bit awkward to say like "a character is bigger than a space". How pawn compare them itself? With comparing the ASCII numbers of them?
|
Yes, just compare characters as integers.
By the way, with strcmp, there is a small logic stupidity. If strings are same, function will return 0. If first string finishes before its completely compared to second one, it will return -1, or 1 if second string finished before first. But, what if one of them is empty (length is 0)? Well function will return 0 (as if the strings were the same).
Re: new in pawno, help -
fiha4155 - 18.06.2017
Quote:
Originally Posted by DRIFT_HUNTER
Yes, just compare characters as integers.
By the way, with strcmp, there is a small logic stupidity. If strings are same, function will return 0. If first string finishes before its completely compared to second one, it will return -1, or 1 if second string finished before first. But, what if one of them is empty (length is 0)? Well function will return 0 (as if the strings were the same).
|
Oh okay then, thank you very much.
Re: new in pawno, help -
BigETI - 18.06.2017
If you wonder why strcmp is implemented to return 0, -1 or 1, it is actually useful for sorting.