OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: Surdus Priest on 05 Dec 2007, 17:43:28

Title: getting letters from string
Post by: Surdus Priest on 05 Dec 2007, 17:43:28
is there a way of for example, getting the letter 'h' into an array from the word 'hello'?

EDIT: another solution to what i am doing would be to know how to cut a string to the length of a set number of characters from the beginning.
Title: Re: getting letters from string
Post by: Trexian on 05 Dec 2007, 19:06:45
These will help? :)

http://kronzky.info/snippets/index.htm
Title: Re: getting letters from string
Post by: Surdus Priest on 05 Dec 2007, 19:40:32
that looks quite good.  its unfortunate that BIS didnt include functions like this.  but anyways, since kron made other good scripts like ups, i cant feel bad about not coming up with something like this myself :D

edit:

i have tried it out and it works very well.  however, it causes a lag from processing so many strings into an array.
i can change this by putting a sleep of 0.001 seconds in the foreach loop. but is there a more effective way of making it process faster?  or do i simply have to put up with the delay until BIS are kind enough to add some string manipulation tools?
Title: Re: getting letters from string
Post by: Spooner on 06 Dec 2007, 11:38:29
The string manipulation "hacks" are quite CPU intensive, which is why they are mostly used by dialog code (where the FPS is a lot less critical). You should be avoiding doing a lot of string processing if you can help it outside of dialogs, but unless you are taking user input, you can usually avoid doing any string processing anyway. If you are doing a large amount of string processing at run time (rather than pre-preparing it at init time), then you may also have to rethink your design.

A for loop with sleep of 0.001 is no faster than a waitUntil loop (it iterates once per frame). If you want to increase the number of string operations you do per second, without doing then all in a block, then only sleep every 10 loops (or whatever).
Title: Re: getting letters from string
Post by: Surdus Priest on 06 Dec 2007, 13:26:56
well, i had already considered pre-processing them in the init. unfortunately that doesnt work, and it must be activated in another script with a 0.01 second delay after the initiation.

i have removed the sleep 0.001 for this because for some reason it thinks theres an error.

also, to do this i have to create a global for each string, which isnt 2 bad i guess, but i do nowadays try to avoid globals as much as possible, which leads me to ask; how many globals can you get away with keeping during a game before saved games become unstable?

edit: im making a dynamic letter-by-letter hint script, and i needed to get and array of strings to do it.

although so far it all looks a bit overkill, its does look very nice now.
Title: Re: getting letters from string
Post by: Loyalguard on 15 Dec 2007, 23:17:48
I was poking around the recent changes page in the Biki today and found some new scripting commands that may accompany 1.09:

http://community.bistudio.com/wiki/toString
http://community.bistudio.com/wiki/toArray
http://community.bistudio.com/wiki/toUpper
http://community.bistudio.com/wiki/toLower

So perhaps we'll get some non-"hack" string functions with the new patch!!!
Title: Re: getting letters from string
Post by: Planck on 15 Dec 2007, 23:26:41
This is true, however they are limited functions and cannot do what some people would like in string functions.

Converting a string to either upper or lower case, or converting a string to ASCII decimal coding or vica versa is all that these functions provide.

Which is nice in itself anyway.   :cool2:

Planck
Title: Re: getting letters from string
Post by: Mandoble on 15 Dec 2007, 23:49:08
Aha, so now we have another worthless set of new commands  :whistle:
Title: Re: getting letters from string
Post by: Spooner on 16 Dec 2007, 00:10:30
Well, although they are limited, they could be used to create most, if not all, usual, string manipulation functions. Wouldn't be as nearly as efficient as if they were native, but considerably better than the crazy, though inspired, workarounds used now. Still, unnecessary work for one of us...
E.g.
Code: [Select]
_strLen = { count (toArray (_this select 0) )};
_length = ["hello!"] call _strLen;
Title: Re: getting letters from string
Post by: Mr.Peanut on 18 Dec 2007, 16:04:16
I agree with spooner. These functions could be easily wrapped to produce some basic string functions such as left, right, mid and len. Convert string to array. Operate on array. Convert back to string. Actually, if these commands are fast enough, they could open some exciting possibilties.

The most important function would be an efficient substring matching function.
Title: Re: getting letters from string
Post by: Planck on 18 Dec 2007, 16:41:08
Aye, ....Is 'nut' in 'peanut'.


Planck