Some usefull array functions

Share your custom panels, brushes, papers... (you need to be registered to read posts in this section)
Post Reply
User avatar
KenC
Posts: 174
Joined: 14 Aug 2009, 09:28
Location: Denmark

Some usefull array functions

Post by KenC »

Thanks to Svengali for giving me a clue on how to parse out values using Parse I came up with a bunch of utillity functions today for working with arrays in george.

Drop the attached file in your george folder and include it at the end of your scripts with:

#Include kc-arrayfunctions.grg

Here's some examples of what can be done.

Code: Select all

tv_getactivetool

//Parse the result variable into array "ParsedArray"
ParseArray(Result)

//After calling "ParseArray" you now have a global array Called "ParsedArray" that you can work with containing
//each space delimited word/value

//Lookup the "step" value in the array
Print "Step: " ArrayLookup("step",1)

//Lookup the second "handle" value in the array using the offset
Print "handle second parameter: " ArrayLookup("handle",2)

//Create string from the array, notice value of second handle parameter.
print StringFromArray()

//Now look for handle in the array and replace the second parameter to 555 just so we can see that it works. (Don't do that in a real script)
ReplaceArrayIndex("handle","555",2)

//Print out again, notice handle second value is now 555
print StringFromArray()

//Print out number of indexes in array. Usefull for creating your own loops through "ParsedArray"
Print ParseArrayLength()

//Reset array
ResetTheArray()

//Print out number of indexes, array is reset so we get zero.
Print ParseArrayLength()

#Include kc-arrayfunctions.grg

Have fun :)
Attachments
kc-arrayfunctions.grg
(5 KiB) Downloaded 164 times
There's no place like ~/
Svengali
Posts: 1553
Joined: 28 Dec 2006, 10:08

Re: Some usefull array functions

Post by Svengali »

Ah, thanks for sharing Ken. Downloading now to have a look! :D

Sven
User avatar
KenC
Posts: 174
Joined: 14 Aug 2009, 09:28
Location: Denmark

Re: Some usefull array functions

Post by KenC »

UPDATE:

Added a few more utillity functions that might come in handy, here's a full list:

Function ParseArray(strResult)

Parse the Result variable into array Named "ParsedArray". This array is available in the calling program so you can manipulate it directly which is sometimes faster. All other functions works off this array so remember to allways call this function before invoking any of the others.

Example to work on brushes:

tv_getactivetool
ParseArray(Result)

The Array ParsedArray now contains every space seperated value from "Result"

These functions are mostly for working with the Result variable, but any space delimited string would work the same.

ParseArrayLength()

Returns the number of indexes in "ParsedArray". Usefull for loops.

ResetTheArray()

Fills "ParsedArray" with EOL terminators, effectively resetting it.

ArrayLookup(strLookup,iOffset)

Checks if strLookup is in the array and returns the string. Since these are returned in Result as "size 10" for example, the iOffset is used to return the next index from where strResult was found. In the case of looking for size as here, 10 would be returned with iOffset set to 1. Some parameters like "Handle" has 2 values so you could set strLookup to "Handle", and "iOffset" to 2 to get the second value.

StringFromArray()

Simply builds a string from the contents of "ParsedArray" and returns that. This makes it super easy to get the index number of a value in "ParsedArray", change that index manually, and then simply call "tv_cmd StringFromArray()" to update a brush.

ReplaceArrayIndex(strLookup,strReplace,iOffset)

Directly replace any index by looking for a parameter name like "opacity", set iOffset to 1 to target the value of opacity and then set strReplace to the new value, then call "tv_cmd StringFromArray()"

RemoveArrayIndex(iPos)

Removes the contents of the index in position iPos in "ParsedArray".

ArrayPosition(strLookup)

Set strLookup to "opacity" for example, then the index in ParsedArray is returned, or 0 if there's no match.

Now you can directly manipulate ParsedArray like this:

tv_getactivetool
ParseArray(Result)
idx=ArrayPosition("opacity")

if Ipos != 0 //Current brush may not have opacity.
ParsedArray[idx+1] = "25"
tv_cmd StringFromArray()
END

TrimArray()

Trims the first index from "ParsedArray" each time it's called.

TrailArray()

Trims the last index from "ParsedArray" each time it's called.

There's lots of comments in the script itself for use of each function.

If any examples are needed just ask.
There's no place like ~/
User avatar
KenC
Posts: 174
Joined: 14 Aug 2009, 09:28
Location: Denmark

Re: Some usefull array functions

Post by KenC »

Maybe I should attach the updated version :oops:
Attachments
kc-arrayfunctions.grg
(8.07 KiB) Downloaded 166 times
There's no place like ~/
Post Reply