It seems like you want to group your data by color and each of your items
consists of a string and two numbers.
Let's assume this items are points (holding an x and y value) that have a
label. (By the way, defining the data makes finding a good structure for it
easier; when you ask a question like this, some background on what your data
represents and what you are trying to do it is helpful).
Assuming this, I'd have an object or dictionary that groups lists of points
by color. So this object or dict would have one entry for each color. This
entry would be an array, that will hold points of a given color.
You could use a class or VO (as Jason suggested) to store each point. You
could also use an Object for this. Using an array is a bad choice if I
understood your problem correctly. Because you're saying "the label is the
first element, x the second and y the third; but it's much easier to give
each of this properties names, so it's more readable and less error prone)
In code, you could try something like this:
public class LabeledPoint {
public var x:int;
public var y:int;
public var label:String;
public function LabeledPoint(label:String = "", x:int = 0, y:int = 0) {
this.label = label;
this.x = x;
this.y = y;
}
override public function toString():String {
return "[LabeledPoint " + label + ", " + x + ", " + y;
}
}
public class PointsDictionary {
private var _dict:Object;
public function PoinstDictionary() {
_dict = {};
}
public function getGroup(label:String):Array {
if(!_dict[label]) {
_dict[label] = [];
}
return _dict[label];
}
public function addPoint(p:LabeledPoint):void {
var group:Array = getGroup(p.label);
group.push(p);
}
}
// use
var points:PointsDictionary = new PointsDictionary();
points.addPoint(new LabeledPoint("red",0,0));
points.addPoint(new LabeledPoint("red",1,0));
points.addPoint(new LabeledPoint("red",2,0));
points.addPoint(new LabeledPoint("blue",0,0));
points.addPoint(new LabeledPoint("blue",1,0));
points.addPoint(new LabeledPoint("blue",2,0));
// get the red points:
var redPoints:Array = points.getGroup("red");
// get the first red point:
var firstRed:LabeledPoint = redPoints[0];
Of course there are many other ways to do this, depending on your
requirements. The above is rather "raw" in that it gives you direct access
to the underlying storage (the arrays) and it's up to you to manipulate them
externally (I just added an addPoint method as a convenience to make the
code less verbose). You might want to prevent duplicates, be able to add an
item at a certain position, remove them, etc, etc. In that case, you could
consider adding other methods or (better) using some more specific data
structures, such as these:
http://sibirjak.com/blog/index.php/collections/as3commons-collections/
Cheers
Juan Pablo Califano
2010/9/3 Lehr, Theodore <[email protected]>
> so if I have a multi-dimensional array - how could I break them into their
> own array? So if I have:
>
> arr[0] = ["red",0,0];
> arr[1] = ["red",1,0];
> arr[2] = ["red",2,0];
> arr[3] = ["blue",0,0];
> arr[4] = ["blue",1,0];
> arr[5] = ["blue",2,0];
>
> What I need to do is break red into it's own array and blue into it's own
> array - and there could be any number of colors....
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders