PDA

View Full Version : Multidimensional array



colorful_tones
April 1st, 2008, 05:44 PM
I have an array/Object:


[0] = mm_name=image1.jpg,mm_path=img/req1,mm_keywords=keyword1
[1] = mm_name=image2.jpg,mm_path=img/req9,mm_keywords=my black cat
[2] = mm_name=image3.jpg,mm_path=img/req7/new,mm_keywords=a brown dog
I want to convert it to a multidimensional array (i think?) so I can reference, like so:


trace(myArray[1]["mm_name"]); // image2.jpg
trace(myArray[2]["mm_keywords"]); //a brown dog
Any suggestions are greatly appreciated!

McGuffin
April 1st, 2008, 06:02 PM
A better way to do this would be:



var myArray:Array = [{mm_name:"image1.jpg", mm_path:"img/req1", mm_keywords:"keyword1"},
{mm_name:"image2.jpg", mm_path:"img/req9", mm_keywords:"my black cat"},
{mm_name:"image3.jpg", mm_path:"img/req7/new", mm_keywords:"a brown dog"}
];
trace(myArray[0].mm_name); // image1.jpg
trace(myArray[2].mm_keywords); // a brown dog


This is using the Array literal ("[ ]") and the Object literal ("{ }") to create an array of objects with the properties and values... the way you were proposing doing it (myArray[1]["mm_name"], you would need to use a Dictionary object, as only integers can be used as Array indices.

Gundark
April 1st, 2008, 06:06 PM
You actually want an array of objects (Arrays aren't supposed to be used for non-integer index values).

So..


var myArray:Array = new Array();
myArray[0] = {mm_name:"image1.jpg",mm_path:"img/req1",mm_keywords:"keyword1"};

//Now you can access like this:
trace(myArray[0].mm_name);
//Or this
trace(myArray[0]["mm_keywords"]);
That should work. Of course if you are going to be dealing with these objects alot you might want to make a class to hold this information and then have an array of classes (rather than just dynamic objects). This could help you with debugging as well as being potentially faster.

Hope that helps!

EDIT: Dang! McGuffin beat me to it!

dzhedzho
April 1st, 2008, 06:28 PM
You cannot have a real multidimensional array, though nothing stops you from creating array of arrays... -
What are you actually trying to do?
Your data looks more suitable for XML, especially if you are searching by keywords and such... the E4X XML is really easy compared wit the AS2 one...


var myXML:XML =
<myXML>
<someNode mm_name="image1.jpg" mm_path="img/req1" mm_keywords="keyword1"/>
<someNode mm_name="image2.jpg" mm_path="img/req9" mm_keywords="my black cat"/>
<someNode mm_name="image3.jpg" mm_path="img/req7/new" mm_keywords="a brown dog"/>
<someNode mm_name="image4.jpg" mm_path="img/req7/new" mm_keywords="a brown dog"/>
</myXML>

var result:XMLList;
var node:*;
result = myXML.someNode.(@mm_keywords=="my black cat");
for each(node in result) {
trace(node.@mm_path+"/"+node.@mm_name);
}
//img/req9/image2.jpg

result = myXML.someNode.(@mm_keywords=="a brown dog");
for each(node in result) {
trace(node.@mm_path+"/"+node.@mm_name);
}
//img/req7/new/image3.jpg
//img/req7/new/image4.jpg



Seems like XML and RegExp classes, are some of the most neglected classes...

Michael Chen
April 1st, 2008, 06:31 PM
Try this...



// AS2

// Assuming initial content is in an array.
var tmp:Array = new Array();
tmp[0] = ["mm_name=image1.jpg","mm_path=img/req1","mm_keywords=keyword"];
tmp[1] = ["mm_name=image2.jpg","mm_path=img/req9","mm_keywords=my black cat"];
tmp[2] = ["mm_name=image3.jpg","mm_path=img/req7/new","mm_keywords=a brown dog"];

// New array.
var myArray:Array = new Array();

// Goes through and extracts the data and assigns value to new array.
for(var a:Number = 0; a < tmp.length; a++) {
myArray[a] = new Array();
for(var b:Number = 0; b < tmp[a].length; b++) {
var splitContent:Array = tmp[a][b].split("=");
myArray[a][splitContent[0]] = splitContent[1];
}
}

// Test
trace(myArray[1]["mm_name"]);
trace(myArray[2]["mm_keywords"]);

colorful_tones
April 2nd, 2008, 10:00 AM
I guess I'm looking for the best way to handle my incoming string of name/value pairs. I guess XML is the best way to go, but how do I parse my string into an XML object?
This is what I'm getting from ASP right now:

uniqueid=1,mm_name=bluehills.jpg,mm_path=img/req1,mm_keywords=keyword1&
uniqueid=2,mm_name=satellite_map.jpg,mm_path=img/req2,mm_keywords=map satellite&uniqueid=3,mm_name=robot.jpg,mm_path=img/req2,mm_keywords=robot black and white

This is what I'd like to work with?

<myXML>
<uniqueid="1">
<mm_name>image1.jpg</mm_name>
<mm_path>img/req1</mm_path>
<mm_keywords>a black cat</mm_keywords>
</uniqueid>
<uniqueid="2">
...
</uniqueid>
</myXML>

colorful_tones
April 2nd, 2008, 05:14 PM
I guess I'm looking for the best way to handle my incoming string of name/value pairs. I guess XML is the best way to go, but how do I parse my string into an XML object?
This is what I'm getting from ASP right now:

uniqueid=1,mm_name=bluehills.jpg,mm_path=img/req1,mm_keywords=keyword1&
uniqueid=2,mm_name=satellite_map.jpg,mm_path=img/req2,mm_keywords=map satellite&uniqueid=3,mm_name=robot.jpg,mm_path=img/req2,mm_keywords=robot black and white

This is what I'd like to work with?

<myXML>
<uniqueid="1">
<mm_name>image1.jpg</mm_name>
<mm_path>img/req1</mm_path>
<mm_keywords>a black cat</mm_keywords>
</uniqueid>
<uniqueid="2">
...
</uniqueid>
</myXML>