Convert String To Int

Hi MEL gurus!

Im sorry I am posting up this question as it probably is quite simple, but I have found no joy by searching! I have created a gui that uses textFieldGrp to get a string of numbers from the user, example "1, 2, 3, 500, 212" etc. I've got that into a string all fine but I need to take then numbers from the string and put them into a int array, so with the example above the array would read int $example [] = {1, 2, 3, 500, 212}. Is there a way of converting the number from the string into a int array, using the comma or white spaces in the strings as markers to signify a new number?

Hope this makes sense! thats not a brilliant description :s
to recap because i dunno if that makes sense, I want to convert a string like "string $example1 = "1, 2, 3, 4"" into a int array, which would look like "int $example2[] = {1, 2, 3, 4}" if i was declaring it.
Alternatively is there a way for textFieldGrp to do this for me?

Thank you very much in advance!

can you not use an intFieldGrp instead?

I was hoping to let the user input all the numbers in one field ie, "1, 2, 3, 4 etc" so as not to limit them as to how many numbers they can input can I do that with intFieldGrp? I'm quite new to this!

Hey kimiko,
You are actually able to recast certain strings as ints. You can also break up a string into an array using a common separator. So, what i think you need to do is take your string, spilt it out into a string array and convert your string array into the int array you're after. Unfortunately, there is no direct way to recast a string array to an int array, but this loop will do that for you...

CODE
int $intArray[];
for($i in stringToStringArray("1, 2, 3, 4, 5",","))
    $intArray[size($intArray)] = $i;

The first line declares an empty int array. The second line initiates a loop, in each iteration of the loop the variable $i represents an element of the string array returned from the stringToStringArray command. Check the MEL docs for an explanation of the stringToStringArray command. The last line recasts the string $i as an int and appends that value to the end of the int array variable. The $intArray[size($intArray)] is just a convenient way to append a value to the next available index in an array.
Hope this helps.

CODE
string $buffer[];
$numTokens = tokenize "1, 2, 3, 4, 5, 6" " ," $buffer;
int $result[];
for($i=0;$i<$numTokens;++$i)
{
  $result[size($result)] = $buffer[$i];
}

the only other thing you'd need to watch for is the user's actual input...you'd need to test or verify that its valid. if they accidentally input a letter or a decimal number, perhaps, these may be invalid inputs for the rest of your script. that's why I suggested an intField...
you could simply run a quick line of error checking to make sure there are only valid integer inputs given. Just something else to be aware of.

what is the grand function of this script going to do? What's the big picture?

its for a university project, we have to make a interface and script to create a keyboard that plays itself, I've got my buttons to create the animation based on a array of numbers for signifying the notes that will be played, 1 = first key, 2 = second key, but i wanted the user to be able to input their own tune, so I need a way to input a series of numbers into a array of any length the user wants. Any other ways to do this than the text field?