2D Arrays

Hello there,
As always, I'm in a hurry!!!

Here's the deal

I have the following info:

Language = English
  word1 = Hello
  word2 = World

Language = Spanish
  word1 = Hola
  word2 = Mundo

(this comes from an XML file)
 And I would like to have it represented in a 2D array like:

Words = new Array;
//Content of Words...
[ [Hello] , [World] ] , [ [Hola] , [Mundo] ]

That way if I need the word 0 of language 0 i'll be able to get it.

I have tried
Words[i][j]= "SomeValue"
but when I trace it, then it says "undefined"

As you might guess I am not very familiar with Arrays.

Please tell me how should i declare, assign and read those kind of arrays.
I need it urgently!!!

Thank you all in advance,

Cërf.
LVL 8
CerfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dutchfoxerCommented:
Oke quick:

a = [[1,2,3],[1,2,3],[1,2,3]];
trace(a); // 1,2,3,1,2,3,1,2,3
a[0][2] = 9;
trace(a); // 1,2,9,1,2,3,1,2,3

You can also think of Objects in Flash MX. new Object();... but above is the quickest!
0
dutchfoxerCommented:
Found this as well:
http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary026.html


And Example of object:

function Book(name, price){
      this.name = name;
      this.price = price;
}

book1 = new Book("Confederacy of Dunces", 19.95);
book2 = new Book("The Floating Opera", 10.95);


Objects tutorial advanced:
http://www.actionscripts.org/tutorials/advanced/3D-Face-Extrusion/index2.shtml
0
CerfAuthor Commented:
hmmm, I have a small problem, The arrays I need to set up are dynamic, since I will be loading them from the XML file. Now this doesn't seem to work unless I initialize them.

Is there a work around?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

blue-genieCommented:
use a loop to run through your xml and add them to your array.
0
dutchfoxerCommented:
I have the following code, maybe it helps. It is part of an XML menu.
Otherwise post the code here you have related to your problem.

subs = menu.firstChild.childNodes.childNodes;
        amount = subs.length;
        for (b=0; b<=amount; b++) {
            _root.attachMovie("submenuItem", "subitem"+b, b+20);
            _root["subitem"+b].nametxt = subs[b].attributes.id;
            _root["subitem"+b].itemUrl = subs[b].attributes.theURL;
        }

0
BuffonCommented:
Can you post here an example of your xml file in order to understand it's exact structure.
0
CerfAuthor Commented:
Hello everybody,
Thanks for replying!

blu, that's what i'm trying to do, but can't get it to work :-(

dutchfoxer, does that menu build the array? or is it somehow hardcoded?

Buffon, there is no need to. My problem is not the XML, in fact that part is working perfectly, I get the values I need and all that stuff, but when I try to put those values into an array... is when my life stinks. lol. anyway, here's an example
rootnode
  words
    language
      word
      word
      word
    language
      word
      word
      word
0
dutchfoxerCommented:
Actually your XML is the array. Once you have read the XML file, then you can use it anywhere in your flash movie. You would not need to transfer all data to an Array. So its hard to understand what you are trying to do.

Anyway. The menu goes through the XML and builds the main-buttons and the sub-buttons. Thats why you see "childnodes", these are the sub-buttons. And with   for (b=0; b<=amount; b++) {   I loop through it. So the menu is not creating an Array, not needed.

See a Tutorial about this here:  http://www.purephotoshop.com/article/95

The dimension is here: items (items = xml.firstChild.childNodes;)   and sub (subs[b].attributes.id;  and   subs[b].attributes.theURL);  ---> id  and URL, in your case Language and Word!?

iattrib = items[1].attributes;
trace(iattrib.language); //Will return the language of 2nd item
0
blue-genieCommented:
as in so
menuXml = new XML();
menuXml.ignoreWhite = true;
menuXml.onLoad = function(success) {
      if (success) {
            menuItem = this.firstChild.childNodes;
            //menuItem = this.firstChild.firstChild.childNodes.attributes;
            trace(menuItem);
            for (var i = 2; i<menuItem.length; i++) {
                  counter=i-2;
                  item = _root.attachMovie("itemClip", "itemClip"+i, i);
                  item._x = 150;
                  item._y = 50*i;
                  item.itemLabel.text = menuItem[i].attributes.text;
                  item.myUrl = menuItem[i].attributes.url;
                  trace(item.myURL add " rul");
                  
                  
      }
};
menuXml.load("myMenu.xml");


work around this.just adjust it as required for you xml structure.


0
CerfAuthor Commented:
Sorry for the lack of information, I need to transfer the data to the array because I will be comparing words that are in the same index and getting stuff like images to load, from another array.

for example, I need to see if the word Hello is Equal to it's spanish "Hola" or "Hallo" in german then I will compare elements of the array like this

let's say English=1 Spanish=2 German=3
and I know that the word I'm asking for, is in position [X,0] of the array, where X is the language

if (SomeVar == ArrayOfWords[1][0]) then { whatever}

If anyone know a better way to do this I'm open to try!
0
CerfAuthor Commented:
Once again, and not meaning to be rude, my problem is not the XML, I am handling it without a problem so far, my issue is the array, or any other structure that has an index I can use to compare elements
0
blue-genieCommented:
dude.
just take your nodes, and using the loop, in your case instead of incrementing by 1, increment by 2 and add them to your array.

0
dutchfoxerCommented:
It would be something like this:

languageItems = xml.firstChild.childNodes;
amount = languageItems.length;
        for (a=0; a<=amount; a++) {
            words = xml.firstChild.childNodes[a].childNodes;
            iattrib = languageItems[a].attributes;
            trace(iattrib.language);
             for(z = 0; z<= subs.length-1; z++){
                  attribs = words[z].attributes;
                  trace(attribs.word);
                  ArrayOfWords[a][z] = attribs.word;
             }
        }

Possibly use eval() in ArrayOfWords[a][z]
0
CerfAuthor Commented:
>> ArrayOfWords[a][z] = attribs.word;

This is the problem, when I assign values to it, it just wont do,
try this

a = new Array(languagesCount)(wordCount)
a = [[1,2,3],[1,2,3],[1,2,3]];
trace(a); // 1,2,3,1,2,3,1,2,3
a[0][2] = 9;
trace(a); // 1,2,9,1,2,3,1,2,3

this works perfectly. Now remove the second line where you initialize the array and try again.
you'll get 2 "undefined"
0
dutchfoxerCommented:
Yes your right, apparently Flash needs to know the dimensions:

a = new Array(languagesCount)(wordCount)
//a = [[1,2,3],[1,2,3],[1,2,3]];
a = [[0],[0]];
a[0][2] = 5;
trace(a); // 0,,5,0
a[1][5] = 9;
trace(a); // 0,,5,0,,,,,9
a[1][6] = 3;
trace(a); // 0,,5,0,,,,,9,3
a[2][6] = 22;
trace(a); // 0,,5,0,,,,,9,3
// -----  22 is NOT in the array
a = new Array(languagesCount)(wordCount)
a = [[1,2,3],[1,2,3],[1,2,3]];
a[1][6] = 8;
trace(a); // 1,2,3,1,2,3,,,,8,1,2,3
a[2][6] = 22;
trace(a); // 1,2,3,1,2,3,,,,8,1,2,3,,,,22
// ------- 22 IS in the array

So you could go through the XML and count the dimensions (amount = languageItems.length;) so you will be able to prepare the Array. Or setup the Array as wide as possible:
a = [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]];  // etc etc

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CerfAuthor Commented:
Well,
I have managed myself to implement a working workaround with all the information you all gave me and some other I found at flashkit, here's the solution for all future reference:

myarray = new Array();

myarray=new Array();
for(i=0; i<3; i++) {
      myarray[i] = new Array();
      for(j=0; j<3; j++) {
            myarray[i][j]=i+j;
      }
}
trace(myarray); //Works : 0,1,2,1,2,3,2,3,4
trace(myarray[2][2]); //Works : 4

somehow strict 2D definition needs to be preset but if you declare an array and then declare an internal element as an array you'll have also a 2D array that you can access as you prefer :-)

I'm happy now

Thank you all for sticking to the threat!!!

Cërf.
0
dutchfoxerCommented:
You can take another approach. Look at this page:
http://www.informit.com/articles/article.asp?p=102159&redir=1

The example in "Listing 4.3 Objects Containing Arrays Nested in an Array" is made for you  :)

Most important is the usage of actionscript "push".
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.