PDA

View Full Version : Remove item in Array output


marxseven
03-25-2003, 09:54 PM
I'm trying to output the values from an array then subtract the value that the item is currently on. Does anyone know how to do this.

For example (this code has been edited to save space!):
My array is being pulled from a text file ->

// this is where it's split it up

labelArray = this.mainMenu.split(",");
numLabels = labelArray.length;

//Here it's being duplicated using the labels in the "labelArray"
for (var i = 0; i<numLabels && i<numLinks; i++) {
name_mc.duplicateMovieClip(labelArray[i], i);
_root[labelArray[i]].name_txt.text = labelArray[i];
_root[labelArray[i]]._y = (i*name_mc._height)+name_mc._y;

//THIS IS THE PROBLEM!!
// the statement below gives me the correct name of the array element, works fine
thisLabel = this._name;

// This gives me all the elements in my array, works fine
trace(labelArray);

//How can I subtract "thisLabel" from "labelArray" and not have it removed from the ARRAY!!
//THIS DOESN'T WORK
labelMinus = eval(thisLabel - labelArray);
trace(labelMinus);


Does anyone have any ideas? Thanks in advance!

:confused:

senocular
03-25-2003, 10:15 PM
what do you mean by "subtract" ?

marxseven
03-26-2003, 12:35 PM
let's say this is the array [orange,apple,grape,plum];
with the above script it takes those items and makes them into MC's. When you click on one of those MC's it tells you the name of the element within the array you are clicking on.

Let's say it is "grape". I then want to output a string that has the current array minus what I clicked on (with permanently removing it from the array. In this case, it should output "orange,apple,plum".

Any Ideas?
Thanks again!

CyanBlue
03-26-2003, 12:44 PM
Howdy...

Easiest thing I can think of...

Duplicate the array and take the selected on out of that duplicated array and show the result as you wish...

???

marxseven
03-26-2003, 12:54 PM
Actually, when you remove an item from this array it kills the whole array. I don't know why... yet. So duplicating it, will probably not quite work. Thanks nonetheless.

magicwand
03-26-2003, 01:07 PM
on (press) {
for (i=_root.selectedArray.length-1; i>=0; i--) {
if (_root.selectedArray[i]!="hoodykid") {
pushed = _root.temp.push(_root.selectedArray[i]);
popped = _root.selectedArray.pop();
} else {
popped = _root.selectedArray.pop();
}
}
for (i=_root.temp.length-1; i>=0; i--) {
pushed = _root.selectedArray.push(_root.temp[i]);
poped = _root.temp.pop();
}
}

i think this is the code i used to del certain item, namely 'hoodykid', from _root.selectedArray.
hope it help

marxseven
03-26-2003, 01:22 PM
Very Close Magicwand. Here's the code, I inserted:
thisLabel = this._name;
for (i=_root.labelArray.length-1; i>=0; i--) {
if (_root.labelArray[i] != thisLabel) {
pushed = _root.temp.push(_root.labelArray[i]);
popped = _root.labelArray.pop();
trace(labelArray);
} else {
popped = _root.labelArray.pop();
}
}
for (i=_root.temp.length-1; i>=0; i--) {
pushed = _root.labelArray.push(_root.temp[i]);
poped = _root.temp.pop();
}


Here's what the trace returned:

Taco,Fruit,Cheese,Bread,Wine
Taco,Fruit,Cheese,Bread
Taco,Fruit,Cheese
Taco

From selecting one item. It appears to go through every item and then gets rid of everything. Bizarre.

magicwand
03-26-2003, 01:29 PM
thisLabel = this._name;
for (i=_root.labelArray.length-1; i>=0; i--) {
if (_root.labelArray[i] != thisLabel) {
pushed = _root.temp.push(_root.labelArray[i]);
popped = _root.labelArray.pop();
trace(labelArray);
} else {
popped = _root.labelArray.pop();
}
}
for (i=_root.temp.length-1; i>=0; i--) {
pushed = _root.labelArray.push(_root.temp[i]);
poped = _root.temp.pop();
}

code will look fine after second for loop.
it reconstruct original array from temp.
trace lableArray after second for loop.

SwampThang
12-17-2006, 10:59 PM
I know this is a really long time to wait to post but here's a very simple solution if you're just trying to remove a specific item from an array and keep the remainder of the array in the same order. You should be able to send any array (won't work with multidimensional arrays) and any item to remove from it and get back the original array without the item.


theArray = ["How", "now", "ye", "ole", "brown", "cow"];

/* create a new array called 'newArr' to store the 'new' array in
after removing the item you wish to remove and call the function.
looking for "brown" to remove it from the array. */

newArr = remItem(theArray,"brown");
trace(newArr);
// traces --> How,now,ye,ole,cow

// REMOVE ITEM FROM ARRAY FUNCTION
function remItem(arr, item){
tempArray = [];
for (i=arr.length-1; i>=0; i--) {
if (arr[i] != item) {
tempArray.push(arr[i]);
}
}
tempArray.reverse();
arr = tempArray;
return(arr);
}


Sometimes we tend to get a little too complicated. I know I do. Hope this helps somebody out there.