[Include] Segment 1.0
#1

Use: Able to select a single group of characters from a string.


Usage: Segment(string[], num)


Example:
Код:
new string[256], word[64];
format(string, sizeof(string), "This is an example string");
word = Segment(string, 4);
In this example "Segment" would return the word "example" from the string "This is an example string".
The variable "word" would store this variable then.


Code: Just paste it into your script. It isn't a very big include, but it is useful.
Код:
stock Segment(string[], num)
{
	new SegBeg[256], SegEnd[256], ComSeg, SegUsed[256], output[32][64];
	SegBeg[0] = 0; SegUsed[0] = 1;
	for(new i = 0; i < strlen(string); i++)
	{
	    if(string[i] == 32)
	    {
	        SegEnd[ComSeg] = i;
	        ComSeg++;
	        SegUsed[ComSeg] = 1;
	        SegBeg[ComSeg] = i + 1;
	     }
	}
	SegEnd[ComSeg] = strlen(string) - 1;
	for(new i = 0; i <= ComSeg; i++) if(SegUsed[i] == 1) strmid(output[i], string, SegBeg[i], SegEnd[i], 64);
	return output[num];
}
Limitations: This include is only able to pickup 32 segments in a string. Each string can be up to 64 characters.
These limitations could be changed but if numbers are set up to high this will not work.


Further Info: This could be useful in making commands. Please give me credit for making this.
Reply
#2

good work
like, is not very limited so it is good
congratulations
Reply
#3

Your function eats 2816 cells... I've scripted this in like 15 minutes and it seems to be more optimized:

pawn Код:
stock Segment(string[], num, len = sizeof string)
{
    new cnum, onum, output[64];
    for(new i; i != len; i++)
    {
        if(string[i] == ' ')
        {
            cnum++;
        }
        else if(cnum == num - 1)
        {
            output[onum] = string[i];
            onum++;
        }
        else if(cnum > num) break;
    }
    return output;
}
Reply
#4

@AustinJ: Good!
@CaHbKo: Perfect!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)