XM Magdalena 3D print, GGeorgy (3D)
LC #42 Pipers Alley

View Full Version : adding and substracting from arrays

01-08-2003, 12:59 AM
Ok.. one more stupid question.

How do I substract and add things to an array.

I've got:

string $list = ("a", "b", "c");

how do I add "d" to it and how do I get rid of "b"




01-08-2003, 07:49 AM
stringArrayCatenate -> adds 2 string arrays together.
stringArrayRemove -> removes one array from another.


01-08-2003, 06:00 PM

Well.. no.. 'cause "d" wouldn't be an array, it's just one item IN the array... so I can't use those commands 'cause they only work between two arrays :shrug:


01-08-2003, 08:13 PM
string $list = ("a", "b", "c");

Your syntax is wrong here for a string. It looks like an array but with () instead of the correct {}.

I would do this then:

string $list = ("a b c");
print ($list + "\n");
string $d = " d";
$list += $d;
print ($list + "\n");
string $newString = `substitute $d $list ""`;
print ($newString + "\n");


01-08-2003, 09:01 PM
Hhmm.. Ok sorry.. I should have said:

string $list[] = {"a", "b", "c"};

What you are talking about is just a string, and adding more to the string.

What I'm trying to do is adding an extra element to an array.
like $list[3] = "d";

and I want to be able to remove just the element "b" (or $list[1])

I'm not working with strings, but with arrays.. (it just happens that in this case, the elements in the array are strings.


01-08-2003, 10:04 PM
If you can, then try and put it into a new string array:

string $listA[] = {"a", "b", "c", "d", "e"};
string $listB[1] = {"c"};
string $listC[] = `stringArrayRemove $listB $listA`;

Otherwise, I donīt know how to reduce the size of an array (or even if itīs possible for that matter).


01-08-2003, 10:17 PM
Yeah.. I ended up going around it and doing something similar to that... it's just not nice and messy code...

I just wish I could have been able to grab the item take it out and stomp it with my feet!! :D

Oh well..

I thought of a way to do it, but it's in C++ and I'm not sure if I have that much control in MEL

regardless.. MEL rules :)


01-08-2003, 10:17 PM
Hey Goosh,

Give this a go. I didn't spend a ton of time debugging it, but it seems to work for simple situations of deleting and adding to an array.

$action = "delete" or "add"
$passedArray = array to manipulate
$newString = the string to remove or add to the array
Returns string array.

global proc string[] arrayManip(string $action, string $passedArray[], string $newString)
string $passedItem;
string $returnedArray[];
string $flattenedString;
int $tokenCount;
string $token = "=|=";

// first, parse array
if (size($passedArray) != 0)
for ($passedItem in $passedArray)
if (($action == "delete") && ($passedItem == $newString))
$flattenedString = $flattenedString;
$flattenedString = $flattenedString + $token + $passedItem;
error("array passed is empty\n");

if ($action == "add")
$flattenedString = $flattenedString + $token + $newString;

$tokenCount = `tokenize $flattenedString $token $returnedArray`;

return $returnedArray;


Let me know if you have problems with it or have questions about it.


01-09-2003, 12:02 AM
Otherwise, I donīt know how to reduce the size of an array

Actually itīs quite easy. Create a new array, clear the old one, then rebuild the old one with the new items:

string $listA[] = {"a", "b", "c", "d", "e"};
string $listB[1] = {"c"};
string $listC[] = `stringArrayRemove $listB $listA`;

clear $listA;
int $i = 0;
for ($item in $listC)
$listA[$i] = $item;
print $listA;

Quick and fairly clean.

01-09-2003, 10:57 PM
For deleting, put the element in an array of its own and use the stringArrayRemove command. For adding items to the end of an array you can simply do:

string $listItems [] = {"a", "b", "c"};

$listItems [size ($listItems)] = "d";

Hope this helps,
Michael Duffy

01-09-2003, 11:33 PM

That easy eh...

thanks Michael..

Thanks everybody actually...


CGTalk Moderation
01-14-2006, 04:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.