View Full Version : adding and substracting from arrays
goosh 01-07-2003, 11:59 PM 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"
:hmm:
Thanks
G
|
|
dwalden74
01-08-2003, 06:49 AM
stringArrayCatenate -> adds 2 string arrays together.
stringArrayRemove -> removes one array from another.
:beer:
David
goosh
01-08-2003, 05:00 PM
eerr...
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:
Goosh
dwalden74
01-08-2003, 07: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");
:beer:
David
goosh
01-08-2003, 08: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.
Goosh
dwalden74
01-08-2003, 09: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).
:beer:
David
goosh
01-08-2003, 09: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 :)
G
ACFred
01-08-2003, 09: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.
Usage:
$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;
else
$flattenedString = $flattenedString + $token + $passedItem;
}
else
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.
Alec
dwalden74
01-08-2003, 11:02 PM
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;
$i++;
}
print $listA;
Quick and fairly clean.
MDuffy
01-09-2003, 09: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
mduffy@ionet.net
goosh
01-09-2003, 10:33 PM
:bounce:
That easy eh...
thanks Michael..
Thanks everybody actually...
Goosh
CGTalk Moderation
01-14-2006, 03: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.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.