2 registered members (7th_zorro, dr_panther),
724
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Split question.
#217371
07/22/08 20:48
07/22/08 20:48
|
Joined: May 2007
Posts: 185 Netherlands
SurudoiRyu
OP
Member
|
OP
Member
Joined: May 2007
Posts: 185
Netherlands
|
Hi, i was wondering if there is a command to split an array? Like there is in Python Scripting. Like the array is filled with: MyArray = ["hello,there,all"] If i acces my array now it will look like MyArray[0] = hello,there,all Now i want to split the array everywhere where the "," is. So the array becomes: MyArray[0] = hello MyArray[1] = there MyArray[2] = all Can someone help me out ? Greetzzz,
-The Dragon's Eye is alway's watching you!-
|
|
|
Re: Split question.
[Re: Joozey]
#217634
07/24/08 07:24
07/24/08 07:24
|
Joined: May 2007
Posts: 185 Netherlands
SurudoiRyu
OP
Member
|
OP
Member
Joined: May 2007
Posts: 185
Netherlands
|
Hmm, ok Python is aswell programming language im working with it on my work. btw, Let me explain: I have a Database connected and got a cell in it i need It is filled with: "Hallo, daaro, allemaal" Now i set it in a string but it will be just like the Database. I want to split the cell whereever is a comma "," So i get an array with 3 items MyArray[0] = "Hallo" MyArray[1] = "daaro" MyArray[2] = "allemaal" Can u help me a little bit with it ? Thnx allready,
-The Dragon's Eye is alway's watching you!-
|
|
|
Re: Split question.
[Re: SurudoiRyu]
#217679
07/24/08 11:28
07/24/08 11:28
|
Joined: Aug 2005
Posts: 1,230
MichaelGale
Serious User
|
Serious User
Joined: Aug 2005
Posts: 1,230
|
Fairly simple actually. Firstly you iterate through all chars in the source string and count the occurrences of the separator char. Then you allocate a new string array (num_separator_chars + 1). Finally you iterate a second time through all characters of the string and append them to the new string array.
Another way would be to use a dynamic array/list, but I am not quite sure which method is the faster one.
Your friendly mod is at your service.
|
|
|
Re: Split question.
[Re: MichaelGale]
#217682
07/24/08 11:38
07/24/08 11:38
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Hmm, ok Python is aswell programming language wink im working with it on my work. Oh yes, I switched python with perl, my bad. So my answer was practically nonsense. But nevertheless you have to program it yourself . Michael's idea about a dynamic array/list is possible, and I think it's alot faster since you only make use of pointers after the mysql library allocated the resultset. I am developping an sql plugin that returns a dynamic multidimensional array with various data from columns (integers, strings, everything), and also any error message, but it's not yet publicly available. It works quite well though. If you have time/are interested in trying, contact me .
Last edited by Joozey; 07/24/08 11:39.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Split question.
[Re: Joozey]
#217799
07/24/08 21:26
07/24/08 21:26
|
Joined: May 2007
Posts: 185 Netherlands
SurudoiRyu
OP
Member
|
OP
Member
Joined: May 2007
Posts: 185
Netherlands
|
Possible to do it like new_string = "Hallo,allemaal,daaro"; my_text.string[3] = new_string; ?
Joozey add me at msn if u want (pm me for my msn)
-The Dragon's Eye is alway's watching you!-
|
|
|
Re: Split question.
[Re: SurudoiRyu]
#218196
07/27/08 03:23
07/27/08 03:23
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Heres the code you're after, but please forgive its "quick and dirty"ness as I threw it together in half an hour. Its limitations are as follows. 1) Any data already in the 'Return' array is overwritten and/or lost and may be classified as a memory leak. 2) Hard-coded to be COMMA delimiters ONLY. (but you could change that yourself if you want to, it only appears on one line) 3) The 'Return' array does NOT get resized so if its was declared too big and you dont stop at the returned index-count, or if it was initially declared too small, run-time errors MAY result (data dependant !!BAD!!). 4) If the source string ENDS with a comma, the last empty item will be ignored, so add a space after the comma and it will be retained as a single space character string. So this function will be useful if youve got a rough idea how many entries there will be. But unsafe to use of you are reading unknown lengths of data(say from a externally generated file). I'll post it as a starting point for others to work from. So no usage rights are required. Heres the routine itself //
function StringSplit(STRING* *ReturnArray, STRING* SourceString)
{
if(str_len(SourceString)==0) { printf("StringSplit-->Invalid SourceData"); return; }
var ItemCount=0; var tmp;
STRING* TempData = str_create(SourceString);
//
while(str_len(TempData)>0) // Build Data Array
{
ReturnArray[ItemCount] = str_create(TempData); //Initialise THIS item
tmp = str_stri(TempData, ","); //Find Delimiter
if(tmp!=0)
{ str_trunc(ReturnArray[ItemCount], str_len(TempData) - tmp + 1); // Remove Delimiter and beyond from Item
str_clip(TempData, tmp); } // Remove "Used" TempData
else { str_cpy(TempData,""); }
if(str_len(TempData)>0) { ItemCount += 1; }
}
return(ItemCount+1);
}
// And heres some code to show how its used and to see it in action, just open the ACKLOG.TXT when you've run the code. ...
STRING* ResultArray[50];
var Count = StringSplit(ResultArray,"Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8,Entry9,Entry10");
//
STRING* tmpStr = "#100";
diag("\n\nStringSplit Returned ");
diag(str_for_num(tmpStr,Count));
diag(" Entries.\n");
var cnt;
for (cnt=0; cnt<Count; cnt++)
{
diag(str_for_num(tmpStr,cnt)); diag("=\"");
diag(ResultArray[cnt]); diag("\"\n");
}
... Enjoy!
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Split question.
[Re: EvilSOB]
#218197
07/27/08 03:37
07/27/08 03:37
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
As you can see from my above coding, there are several dangers in passing pre-declared arrays.
As you can also see, the core of the code is quite small, so I would STRONGLY suggest you actually include the code WITHIN the function that declares the result array and bypass all the serious risks with array passing. I'll try to get an example posted later today or tomorrow.
Either way, you should also call a str_remove() on each array entry after it is used, or on every entry in the array before exiting or each un-removed entry will become "leaked" memory.
Best of luck, and feel free to ask questions.
PS EXPERTS - sideline question here, please clarify my understanding here. If you declare structures and/or array pointers as local variables, and fill them using creation functions (eg str_create), does the contents of those pointers get purged on the function closing/terminating or does the contents become "leaked memory" (as I understand is the case). Does it vary between pointer types or anything if I am wrong? Thanks
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Split question.
[Re: EvilSOB]
#218344
07/28/08 07:30
07/28/08 07:30
|
Joined: May 2007
Posts: 185 Netherlands
SurudoiRyu
OP
Member
|
OP
Member
Joined: May 2007
Posts: 185
Netherlands
|
Wow thnx EvilSOB, I will look into your script and gonna do some test's with it. Maybe i can change some things in it.
Thnx for the example and de help, Hope i can go bit further now.
Greetzzz,
-The Dragon's Eye is alway's watching you!-
|
|
|
|