PDA

View Full Version : Remove an element from an array?


vtcgman
02-20-2007, 08:50 PM
So this seems really dumb but I can seem to figure out how to do it? Say I have an array with a number of elements in it and I want to remove the element at index 3, how can I do that? The only way that I can think of, that's horribly inefficient, is to copy the array to a temporary array skipping that index and clear the old array and copy it back. Any thoughts? Thanks.

brubin
02-20-2007, 11:40 PM
hi,

i got news for you, one is not so good, the other makes it okay:

1st:
there's no way around the 'horribly inefficient', as you call it,
BUT:
the good news is, this function exists!
A|W caught on late by adding these scripts, because they were around long before in the public domain. they can be found in "<maya-app-location>\scripts\others\":


stringArrayRemove.mel
stringArrayRemoveAtIndex.mel
stringArrayRemoveDuplicates.mel
stringArrayRemoveExact.mel

...are just a few, there are more dealing with other problems in array manipulations.
funny thing is, there are no "intArray..." or "floarArray..." -functions, but based on the scripts provided by A|W those can easily written by anyone, if needed.

HIH
s.

vtcgman
02-21-2007, 01:02 AM
Well that's a bummer! Thanks for the reply and the tip on the additional scripts, there's some interesting things in there!

ljilekor
02-21-2007, 01:46 AM
Although it lacks index-based manipulation, Maya provides nice functions for strings. For other types and index-based edits, this might do the thing.

This xample is for floats.
$ID represents the index where you want to add/remove an element.


// Lowlifescum array manipulations



///INSERT //////////////

proc float[] lls_insertIDToFloatArray(float $fArray[], float $val, int $ID)
{
int $size = size($fArray);
for($i=$size;$i>$ID;$i--){
$fArray[$i]=$fArray[$i-1];
}
$fArray[$ID]=$val;
return $fArray;
};

///REMOVE ////////////////
proc float[] lls_removeIDFromFloatArray(float $fArray[], int $ID)
{
float $fResArray[];
int $size = size($fArray);
int $cnt=0;
for($i=0;$i<$size;$i++){
if($i!=$ID){
$fResArray[$cnt]=$fArray[$i];
$cnt++;
}
}
return $fResArray;
};





Voilą

Soviut
02-22-2007, 09:34 PM
While it may seem pretty inefficient, this is basically how every programming language handles arrays. Some do it more under the hood, but typically this is what happens.

CGTalk Moderation
02-22-2007, 09:34 PM
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.