Forum Topic: Finding data in an Array

(73 views • 6 replies)

This topic is 1 page long.

<< < > >>
Questioning

roboprez

Reply To Post Reply & Quote

Posted at: 7/24/09 09:42 PM

roboprez DARK LEVEL 07

Sign-Up: 08/25/07

Posts: 181

Okay I have an array with quite a bit of data in it
I want to find one value and push it too another array
how would I do this? is there some kind of function to search the values and get the index number?

(if you don't understand my problem here's an example)
I have an array(8,5,3,6,21,76,8)
I want to take the value of 21, cut it out of this array and put onto another one
the first array should be(8,5,3,6,76,8) and the 2nd just (21)

~Last FM~(Music) ~DeviantART~(Flash)


None

TheSwindler

Reply To Post Reply & Quote

Posted at: 7/24/09 09:49 PM

TheSwindler LIGHT LEVEL 06

Sign-Up: 07/22/09

Posts: 89

You can always use a for loop and an if statement.


None

KaynSlamdyke

Reply To Post Reply & Quote

Posted at: 7/24/09 09:58 PM

KaynSlamdyke LIGHT LEVEL 16

Sign-Up: 06/25/04

Posts: 4,926

At 7/24/09 09:42 PM, roboprez wrote: Okay I have an array with quite a bit of data in it
I want to find one value and push it too another array
how would I do this? is there some kind of function to search the values and get the index number?

(if you don't understand my problem here's an example)
I have an array(8,5,3,6,21,76,8)
I want to take the value of 21, cut it out of this array and put onto another one
the first array should be(8,5,3,6,76,8) and the 2nd just (21)
var arrayData:Array = [8,5,3,6,21,76,8]
var search:Number = 21;
var result:Number;

for(var i:Number = arrayData.length-1; i>=0 ; i--){ //Reverse searching an array. Since we're deleting stuff, we go backwards, that way we don't skip things by accident.
  if(arrayData[i] == search){
   result = arrayData[i]; //Set result variable to the found value
   arrayData.splice(i, 1); //Deletes one element from position i
   break; //We've found the data. Get out of the for loop. If this line gets removed we continue looping, and that's why we reverse search
  }
}
trace(result); // Returns 21
trace(arrayData); // Returns 8,5,3,6,76,8

Current build for ThreedeeTiles : Monkey
Previous: Lamprey, Mountain Goat (Dead Fork)


None

roboprez

Reply To Post Reply & Quote

Posted at: 7/24/09 09:59 PM

roboprez DARK LEVEL 07

Sign-Up: 08/25/07

Posts: 181

yes but how do I cut the data out of the array?

~Last FM~(Music) ~DeviantART~(Flash)


None

roboprez

Reply To Post Reply & Quote

Posted at: 7/24/09 10:08 PM

roboprez DARK LEVEL 07

Sign-Up: 08/25/07

Posts: 181

sorry I didn't see the last post. Yes using a loop and the splice/push functions do it perfectly! Thanks!

~Last FM~(Music) ~DeviantART~(Flash)


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/24/09 10:09 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/24/09 09:59 PM, roboprez wrote: yes but how do I cut the data out of the array?

He did it in the code he gave you. Splice() removes a value from an array.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

KaynSlamdyke

Reply To Post Reply & Quote

Posted at: 7/24/09 10:15 PM

KaynSlamdyke LIGHT LEVEL 16

Sign-Up: 06/25/04

Posts: 4,926

Gotta love the minute between post gap...

Current build for ThreedeeTiles : Monkey
Previous: Lamprey, Mountain Goat (Dead Fork)


All times are Eastern Standard Time (GMT -5) | Current Time: 04:27 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!