PDA

View Full Version : basic array help



fm47
October 7th, 2007, 04:33 PM
I'm confused on as how arrays work... could someone help me?

Say I wanted to make an array for each employee, included are their names, last names, age, years of experience, and salary.

as far as my understanding goes, it would be like so:

myEmp = new Array ();
myEmp [0] = "George", "Lakeston", 36, 3, "$3,500.00";
myEmp [1] = "Sally", "Morganson", 28, 1, "$2,000.00";
myEmp [2] = "Trace", "Brimer", 20, 0, "$1,250.00";

I'm imagining that if I had a mini search engine and wanted to find George Lakeston, I'd type in his name, make the submitted text in all lower case (how do I do that, btw?), then have it match with the appropriate array by using for command. at 0, it would try to match the modified submitted search, if 0 wasn't it, it would do ++ until it finds a match.

ok so I can call the array by using myEmp [0] to return "George", but what about the rest of the information on myEmp [0]?

At first I did:
myEmp [georgelakeston] = "George", "Lakeston", 36, 3, "$3,500.00";

but that doesn't work either...

btw, sorry if I'm using an older form of AS, I would be glad to learn the new or better ways. I am using flash 8.

Michael Chen
October 7th, 2007, 05:01 PM
You can also have arrays within arrays.

Here is a simple example plus a "search by name"...



var myEmp:Array = new Array ();
myEmp[0] = ["George", "Lakeston", 36, 3, "$3,500.00"];
myEmp[1] = ["Sally", "Morganson", 28, 1, "$2,000.00"];
myEmp[2] = ["Trace", "Brimer", 20, 0, "$1,250.00"];

function searchForEmployee(name:String):Array {
for(var a = 0; a < myEmp.length; a++) {
if(myEmp[a][0] == name) {
return myEmp[a];
}
}
}

// Returns information about George.
trace(searchForEmployee("George"));

fm47
October 7th, 2007, 05:09 PM
hm, OK. So I can see how parts of it works, but some other parts of the coding confuses me. Could you explain these code pieces?

function searchForEmployee(name:String):Array
- I can see that it's a function called searchForEmployee, but what's in the parathensis? Is it a call or a declaration? Also the :Array afterwards... what's that?

if(myEmp[a][0] == name)
- how does the two sets of brackets work? "a" represents the for variable, and "0" represents the array number, but how do they work together?

So far on my search button i have



on(release) {
mySeek = searchBox.text;
//then a line here to make all the text lowercase
for (ii = 0; ii < myEmp.length; ii++) {
if (mySeek == myEmp[ii]) {
aa = ii;
searchBox.text = myEmp[aa];
}
}
}

Michael Chen
October 8th, 2007, 01:06 AM
function searchForEmployee(name:String):Array
- I can see that it's a function called searchForEmployee, but what's in the parathensis? Is it a call or a declaration? Also the :Array afterwards... what's that?

The things inside the parathensis are parameters. Basically passing information to the function. What I've done is passed the name "George" into the function. :Array is what the function returns. Thus it'll return an array when the call is completed. Which is what is passed back to the trace statement.


if(myEmp[a][0] == name)
- how does the two sets of brackets work? "a" represents the for variable, and "0" represents the array number, but how do they work together?

Basically it's a 2D array, which is just arrays within arrays.

Here is a simple example...



var array1:Array = new Array();

array1[0] = [1];
array1[1] = [2,3];
array1[2] = [4,5,6];

// Prints the array1 items, which are arrays.
for(var a = 0; a < array1.length; a++) {
trace(array1[a]);
}

// Prints the array1 items, and also items individually within the array.
for(var a = 0; a < array1.length; a++) {
for(var b = 0; b < array1[a].length; b++) {
trace(array1[a][b]);
}
}

fm47
October 10th, 2007, 01:33 AM
oh my gosh! I finally got it to work. Thank you so much!

I had forogtten to put my arrays in brackets

var emyEmp:Array = new Array()
myEmp [0] = [forgot the outter brackets here] ><!


xie xie ^^ (i'm assuming through the "Chen" part in your name. I'm Taiwanese ^^)