HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Explode function, anyone?

02-24-2004, 09:46 PM#1
LegolasArcher
I had a few minutes of spare time, and wanted to fool around with JASS so I concocted this. You pass it an string divided by dividers. For example:

Quote:
This|sytem|was|made|by|LegolasArcher|on|WC3Campains.com|!

That would be the string and:
Quote:
|

Would be the divider.

Essentially it splits it into the words between the dividers- returning each sepperatly in an array. I designed this after working with PHP's explode function. The global "explode" is where the results are stored. Heres the code:

Code:
function ExplodeString takes string value, string divider returns nothing
    local integer length = StringLength(value)
    local string array chars
    local string array final
    local integer i = 1
    local integer j = 1
    loop
        exitwhen i == 9000
        set udg_explode[i] = ""
        set i = i + 1
    endloop
    set i = 1
    loop
        exitwhen i == length + 1
        set chars[i] = SubStringBJ(value, i, i)
        set i = i + 1
    endloop
    set i = 1
    loop
        exitwhen i == length + 1
        if (chars[i] == divider) then

            set j = j + 1
        else
            set final[j] = final[j] + chars[i]
        endif
        set i = i + 1
        set udg_explode[j] = final[j]
    endloop
endfunction

I'd love some constructive critisism, but please no flaming. Im sort of new at JASS, so I'd love any tips and suggestions

[edit]I know I coul have bypassed "final", but I think its cleaner this way. Also, I may add stuff later, and that may come in handy ;)

Test map:
02-24-2004, 10:13 PM#2
weaaddar
Um Peppar beat you to it a long time ago. Here is the version he made which works identical to yours, actually its slightly better as it'll take as many delimiters as you throw at it:
http://kattana.users.whitehat.dk/viewfunc.php?id=200
And the older version which I use in DT4a:
http://kattana.users.whitehat.dk/viewfunc.php?id=48

Also I don't see the point of using an array for all the chars.Why not just transverse the string using substring, I know there is no charAt function like in java but its not very difficult to use SubString is it? Rather wasteful. You could of just used a temp string instead if you really didn't like writing that all out.
And why is the maximum 9000? Arrays limits are 8192 and strings length max is only 2048. (so your first loop only has to go to 2048). Also your function will probably hit the dreadful call limit because of this wasteful clearing of the array. (Use Runseperate and have that as its own trigger).
Sorry to sound rather offending, anyway good job. I'm not good when it comes to writing string tokenizing algorothms.
02-24-2004, 10:21 PM#3
ph33rb0
Well I'd first like to know the entire purpose of this function. Doesn't seem like it's too useful.

Secondly instead of using a global just make it return the string.
02-24-2004, 10:25 PM#4
weaaddar
String fragmentation is incredibly useful considering inputs.
Imagine you create a string message ran engine.
If you had to explicitly define a case for each command and each of its inputs you would probably go bonkers and hate it much easier.
Also it useful for emulating arrays. See DT4a if you wondering just how "useless" this is.
02-24-2004, 10:28 PM#5
Vexorian
And returning the string isn't a good idea against global variables, althought instead of globals game cache may help.

Globals are better because it will save time when you want to get more than 1 of the tokens in the same process.
02-24-2004, 10:31 PM#6
LegolasArcher
A few questions and comments,:
  • I never thought of that at the time, but now that you mention it it seems obvious (its funny how things do that)
  • I wasnt sure about the max index of an array, I only knew it topped out around 8000-900, but I didnt know the exact number
  • Ive never had any experiance with Runseperate, could you explain this (as I said Im just starting with JASS)

In reply to ph33rb0, i need to split the strings by a caracter, so how would returning 1 string variable help?

BTW doesn't that site not allow user globals? If so,than how could he submit a function with one of them involved- unless im missreading him.

Thanks for the reply, weaaddar, I'll post a new version once i figure out what Runseperate is and how to use it. Thanks for sharing your knowedge ;).

Final thing, I threw that reset loop in on the last minute, since I couldnt decide to use it or not.
02-24-2004, 10:32 PM#7
ph33rb0
Quote:
Imagine you create a string message ran engine.
If you had to explicitly define a case for each command and each of its inputs you would probably go bonkers and hate it much easier.

I don't like console commands in a GUI. I'd rather have the user press escape and have a dialog pop up to issue a command.

As for emulating arrays, well, there's never been a case where I just couldn't use a normal array, but I guess if you say so. :-/
02-24-2004, 10:35 PM#8
Vexorian
This is another example of tokenizing goodness: http://kattana.users.whitehat.dk/viewfunc.php?id=210
02-24-2004, 11:09 PM#9
LegolasArcher
Could someone please address my questions?
02-24-2004, 11:41 PM#10
weaaddar
its a jass vualt function basically you pass it a function which contains commands that you want to run in a seperate thread, to prevent hitting the call limit.
02-24-2004, 11:43 PM#11
LegolasArcher
Ah, thanks. I've never heard it mentioned before.