14 Replies Latest reply on Dec 8, 2008 7:22 AM by fungus13

# Multidimensional arrays - adding, extracting

How do you push values onto an multidimensional ASSOCIATIVE, ARRAY

Consider the array
var questionList:Array = new Array();

I want to be able to extract values as such
question1 = questionList[0][2];
question3 = questionList[2][2];

So I can call the questions and answers using a for loop

e.g. question = ArrayList[2];

When I try setting up the array with a number of variables
The question number, the random number, question, answer
These are all originally stored in separate arrays.

for (var r2:int=0;r2<questionTest.length;r2++)
{
randomQ = randValue[r2];
questionList.push( [r2], [randomQ], [questionTest[randomQ]], [answerTest[randomQ]] ) ;

}

This produces the array with all the values, but
trace("questionList_A = " +questionList[r2]);
Outputs only one field
e.g. questionList[3] = What is the first key of G Major?

trace("questionList[r2,1] = " +questionList[r2][1]); is undefined

If I put
questionList[r2] = [ [r2], [randomQ], [questionTest[randomQ]], [answerTest[randomQ]] ] ;
Then trace("questionList_A = " +questionList[r2]);
Outputs
questionList[r2] = 0,2,What is the first key of E Major?,E
I get all the values now, but they are inseparable

questionList[r2][1]);
Produces TypeError: Error #1010: A term is undefined and has no properties.
at PianoTutorTest2_fla::MainTimeline/testQ()

If I just push the lot on the array I get all the values in sequence

pushes all the values on the array, but
questionList[r2]= What is the first key of A Major?
and
questionList[r2][1] creates a new error - well at least I get variation!
ReferenceError: Error #1069: Property 1 not found on String and there is no default value.
at PianoTutorTest2_fla::MainTimeline/testQ()

There is not a lot on pushing values onto arrays. The adobe help only shows how to put specific data on the array, but doesn't give much information on how to push/put computer generated values on the array.
• ###### 1. Re: Multidimensional arrays - adding, extracting
The call above has been corrupted

I want to call the questions
e.g. question = questionList[2];
• ###### 2. Re: Multidimensional arrays - adding, extracting
I give up

question_i = questionList_i_2

I think you get the drift, for some reason the i disappears on loading
• ###### 3. Re: Multidimensional arrays - adding, extracting
I can answer one problem you're having... when you try to show an array index in a posting, if it happens to be using an "i" as an index, as in... question[ i ], you need to add spaces in the brackets... otherwise it is read an an italics code for the posting.

If you haven't gotten any help by the time my eyes wake up tomorrow, I'll see if I can dig into your posting.
• ###### 4. Re: Multidimensional arrays - adding, extracting
I look forward to your solution.
I'll have to simplify the array to just two values to meet tomorrow's deadline, That way at least every other entry is a question and that should be retrievable by changing the increment.
Still working on the analog timer, though doubt I'll figure it out by tomorrow.
• ###### 5. Re: Multidimensional arrays - adding, extracting
It's a little hard to read your posting, not only because of all the italics showing up, but also because of the different terms and brackets you use everywhere.

But I think I might have an idea of what you are trying to do.... get the question and answer and other possible related info held together in one group for each question.

Here's one way to do it. As soon as you have the values you want to store as a set in the array, assign them as an object.

questionList[ i ] = {qNum: ?, question: ?, answer: ?};

where the ? are the values for the different elements

So when you want to get a question/answer set into play, you only need to deal with a single dimension array

questionList[ i ].qNum.... gives you the number
questionNum[ i ].question.... gives you the question

If I've missed the target for what you are trying to do, then see if you can reduce the explanation a bit.
• ###### 6. Re: Multidimensional arrays - adding, extracting
I gave the options I'd tried, some had a few brackets in them. Sorry about that. Not a lot of documentation on the topic.
But you are on the right track. That looks like what I want.
I've only been writng Actionscript for a few months, so I'm still relatively new to the code. At the moment I'm tangled over calling the questions one at a time and storing the answer. I've done it (almost) for single random values. But what I'm finding is the response array fills without my answering the question. I'm past the "error check" stages as the Actionscript check generally indicates no errors, but the logic is failing.
• ###### 7. Re: Multidimensional arrays - adding, extracting
Ned, You've been so helpful so far I figured you might be able to see the flaw in this code. I think I'm on the right track, but time is running out and I'm not sure if I'll figure it out in time. Compressed a bit here.

for (var qnCount:int = 0; qnCount < questionTest.length; qnCount++)
{if(responseTest.length < questionTest.length)
{if(tally[qnCount] == 0)
{ responseTest.push(keyNote);
qtn = qtn+2;
tbQ.text = questionList[qtn]; tally[qnCount]++;
}//End-if tally
}//End-if responseTest
if(responseTest.length == questionTest.length)
{tbQ.text = "You have completed all the questions";
display_message = score_result(numberCorrect,numberOfQuestions);}
}//End-for qnCount
• ###### 8. Re: Multidimensional arrays - adding, extracting
I've managed to get a Score function to work e.g.
Again somewhat compressed
function score_result(numberCorrect,numberOfQuestions):String
{if ((numberCorrect/numberOfQuestions)*100>80)
{message = "Congratulations, you have achieved over 80% correct";
} else if ((numberCorrect/numberOfQuestions)*100>60)
{message = "You have achieved a score of over 60% correct";
} else if ((numberCorrect/numberOfQuestions)*100>50)
{message = "You have achieved a score of over 50% correct,you need to do some more study";} else
{message = "You have a score of less than 50%, I would suggest a change in career";} return message;}

Meanwhile - I'll try out your array solution
• ###### 9. Re: Multidimensional arrays - adding, extracting
I've got two dynamic boxes on screen one being tbQ the other tbAns. The Ans box should come up giving the score.
e.g. tbAns.text = display_message;
• ###### 10. Re: Multidimensional arrays - adding, extracting
A question
questionList[ i ].qNum.... gives you the number
questionNum[ i ].question.... gives you the question

Do you mean
questionList[ i ].qNum.... gives you the number
questionList[ i ].question.... gives you the question

It makes more sense.

• ###### 11. Re: Multidimensional arrays - adding, extracting
I realise I've left out a bit of code actually. The bit to set number correct/incorrect so calling the function (Scores) was a little premature. But that wasn't a problem as long as I can call and compare the "responseTest" with the "answerTest" and generate the values. It's getting seven answers that's the problem.
• ###### 12. Multidimensional arrays - adding, extracting
You are correct in noticing the error of my ways... the dangers of copy and paste.

It's really hard to read code when it isn't indented nicely, especially when you have nested things inside nested things. Use the attach code option to retain formatting.

At this point, I'm not sure what you are having a problem with. I won't be around for awhile, so you may need to try a new posting for the problems you might be having now... postings with zero responses tend to get attention faster.
• ###### 13. Re: Multidimensional arrays - adding, extracting
I'll do that.
Thanks for your help, so far.
• ###### 14. Re: Multidimensional arrays - adding, extracting
I tried to implement the code for the Array, but it came up with an error.
The two variable array works Ok (eg. Question, Answer)