Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: [programming] finding a specific data type in an array  (Read 2140 times)

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile
[programming] finding a specific data type in an array
« on: December 22, 2012, 05:38:02 PM »

Hi all,

So I ran into another problem in my programming project (thanks to thaago for solving my last one :) ), hoping someone here might know what to do. The code is in actionscript 3 but it's similar enough to java that any code that work there probably works here too

Say there's an array as follows:

var a: Array = [Number:3.14, int:5, string:"Hello World!!"]

and I want to check which one of these is the data type I want, preferably its position in the array.

Example: myFunction(a, int) should return 1, myFunction(a, string) should return 2.

Now I suppose I can just do this with a for loop going through everything in the array and check if it is the datatype, but is there a more elegant way to go about this?

thanks in advance

gunny
Logged
mmm.... tartiflette

naufrago

  • Admiral
  • *****
  • Posts: 511
    • View Profile
Re: [programming] finding a specific data type in an array
« Reply #1 on: December 22, 2012, 05:48:29 PM »

If you're looking for a particular piece of data in an unsorted array and you don't know exactly where that data is within the array, then the only way to be sure to find that data is to go through everything. A linear search is your best option (in other words, what you suggested is appropriate).
Logged

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile
Re: [programming] finding a specific data type in an array
« Reply #2 on: December 22, 2012, 06:54:05 PM »

ok fair enough....

it clutters up the code so much though, maybe I should make it a function on the base class, just so it's easier to read...
Logged
mmm.... tartiflette

TJJ

  • Admiral
  • *****
  • Posts: 1905
    • View Profile
Re: [programming] finding a specific data type in an array
« Reply #3 on: December 23, 2012, 04:14:15 AM »

I have to ask; why are you storing mixed types together in the same array?
Logged

Sproginator

  • Admiral
  • *****
  • Posts: 3592
  • Forum Ancient
    • View Profile
Re: [programming] finding a specific data type in an array
« Reply #4 on: December 23, 2012, 06:17:34 AM »

Can't you use the OfType method? Type in w3schools into google an look through there
Logged
A person who's never made a mistake, never tried anything new
- Albert Einstein

As long as we don't quit, we haven't failed
- Jamie Fristrom (Programmer for Spiderman2 & Lead Developer for Energy Hook)

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile
Re: [programming] finding a specific data type in an array
« Reply #5 on: December 23, 2012, 05:56:38 PM »

I have to ask; why are you storing mixed types together in the same array?

I gave most objects in my game a childarray (an array containing all objects under it)

in this case, it would mean for example aircrafts, ships, and ground units are in the same array. And I want some things to only effect one type of these. (so it's not actually built in datatypes like int, but classes I made that all eventually subclass to movieclip)

Can't you use the OfType method? Type in w3schools into google an look through there

googled it, doesn't seem like it's in AS3, shame.....
Logged
mmm.... tartiflette