SA-MP Forums Archive
[Include] Segment 1.0 - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] Segment 1.0 (/showthread.php?tid=272625)



Segment 1.0 - AustinJ - 29.07.2011

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.



Re: Segment 1.0 - DartakousLien - 29.07.2011

good work
like, is not very limited so it is good
congratulations


Re: Segment 1.0 - CaHbKo - 29.07.2011

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;
}



AW: Segment 1.0 - Pablo Borsellino - 29.07.2011

@AustinJ: Good!
@CaHbKo: Perfect!