00:00
00:00
Newgrounds Background Image Theme

Patrick8008 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Response to A S: Arrays 2005-11-20 06:24:16


At 11/17/05 02:27 PM, authorblues wrote:
to call a whole array, you just select the index of the outermost array:
nest[1] would return [1, 0, 0]

but to call a value from inside a nested array, you select the index of that array youve just indexed:
nest[1][0] would return 1

hope that helps...

id dose but is there a wal to acses it by collems like down not across?
thanks for help


I have done the deed. Didst thou not hear a noise?

BBS Signature

Response to A S: Arrays 2005-11-21 00:58:03


At 11/20/05 06:24 AM, glompho wrote: id dose but is there a wal to acses it by collems like down not across?
thanks for help

holy crap, you are the worst speller i have EVER seen on newgrounds!


snyggys

Response to A S: Arrays 2005-11-21 01:05:05


ok, im having some trouble understanding this whole nested array thing. i took a crack at it, and its not quite working the way i hoped. see, i was hoping to be able to access an array within my array by using the nested array's name.

names = new Array("Stevie");
responses = new Array("I died");
array = [[names], [responses]];
trace(array[0]+"\n"+array[1]);
trace("Hey there, " + array[names[0]] + " and " + array[names][1] + "!");

as you can see, i have 3 arrays; "array", and "names" and "responses" are nested within them. i was hoping to access the data within those arrays like array[name[1]] or something, and instead, i can only access it like array[0]. am i nesting them wrong, or something?


snyggys

Response to A S: Arrays 2005-11-21 01:12:57


geeez... communitymx.com havnt been there in AGES

Response to A S: Arrays 2005-11-21 01:18:36


At 11/21/05 01:05 AM, 2k_rammerizkool wrote: names = new Array("Stevie");
responses = new Array("I died");
array = [[names], [responses]];
trace(array[0]+"\n"+array[1]);
trace("Hey there, " + array[names[0]] + " and " + array[names][1] + "!");

firstly, dont ever use reserved words like "array" for variable names. secondly, its just a syntax error, but you have the right idea. here is how i would edit your code.

var names:Array = new Array("Stevie", "Billy");
var responses:Array = new Array("I died");
var stuff:Array = new Array(names, responses);
trace(stuff[0]+newline+stuff[1]);
trace("Hey there, " + stuff[0][0] + " and " + stuff[0][1] + "!");


BBS Signature

Response to A S: Arrays 2005-11-21 01:20:42


At 11/21/05 01:18 AM, authorblues wrote: firstly, dont ever use reserved words like "array" for variable names. secondly, its just a syntax error, but you have the right idea. here is how i would edit your code.

its not reserved. Array is, but array isnt.

var names:Array = new Array("Stevie", "Billy");
var responses:Array = new Array("I died");
var stuff:Array = new Array(names, responses);
trace(stuff[0]+newline+stuff[1]);
trace("Hey there, " + stuff[0][0] + " and " + stuff[0][1] + "!");

-_- did you read my post? i said that i wanted a way to do something like stuff[name][0] or whatever. not with just numbers.


snyggys

Response to A S: Arrays 2005-11-21 01:23:30


At 11/21/05 01:20 AM, 2k_rammerizkool wrote: its not reserved. Array is, but array isnt.

if that IS true, it isnt a good habit to get into. it is better to just stay away.

-_- did you read my post? i said that i wanted a way to do something like stuff[name][0] or whatever. not with just numbers.

not only is that not possible, but that defeats the point of having a nested array. if you wanted to do that, you could just as easily call the information using name[0]. why would you want to be that specific? that isnt what nested arrays are for.


BBS Signature

Response to A S: Arrays 2005-11-21 01:33:24


At 11/21/05 01:23 AM, authorblues wrote:
At 11/21/05 01:20 AM, 2k_rammerizkool wrote: its not reserved. Array is, but array isnt.
if that IS true, it isnt a good habit to get into. it is better to just stay away.

it is true. but i know its a bad habit, and it was only for the purpose of a small test. i usually dont do that

-_- did you read my post? i said that i wanted a way to do something like stuff[name][0] or whatever. not with just numbers.
not only is that not possible, but that defeats the point of having a nested array. if you wanted to do that, you could just as easily call the information using name[0]. why would you want to be that specific? that isnt what nested arrays are for.

well, ok, now that i know its not possible, MEH. and yeah, i guess im just not thinking straight. its 11:30 pm right now (where i live), so im just a bit sleepy. whatever. g'night.


snyggys

Response to A S: Arrays 2005-11-23 15:41:24


At 11/21/05 01:05 AM, 2k_rammerizkool wrote:

stuff:

you could have done this with simalar resalts

names = new Array("Stevie");
responses = new Array("I died");
array = [[("Stevie","Bob"], ["lived","Died"]];
trace(array[0]+"\n"+array[1]);
trace("Hey there, " + array[0][0] + " and " + array[0][1] + "!");

you define nested arrays inside the primary array


I have done the deed. Didst thou not hear a noise?

BBS Signature

Response to A S: Arrays 2005-11-26 07:09:32


hi, i know this may sound pretty stupid, but how would you call a random variable from an array, to display in a dynamic text box?

thanks in advance
sheffgb

ps. great tutorial

Response to A S: Arrays 2005-11-26 10:28:58


At 11/26/05 07:09 AM, sheffgb wrote: how would you call a random variable from an array, to display in a dynamic text box?

do you mean pull a random piece of information from the array?

_root.txtbot.text = myArray[Math.floor(Math.random()*myArray.l
ength))]


BBS Signature

Response to A S: Arrays 2005-11-27 09:59:39


i was woundering... is it possible to have arrays inside arrays inside arrays (yes 3 times), if it is... whats the code

Response to A S: Arrays 2005-11-27 10:23:59


[[[var1, var2], [var3, var4]], [[var5, var6], [var7, var8]]];


BBS Signature

Response to A S: Arrays 2005-12-05 18:41:23


more array questions. i have this variable "rand" that is set to a random number according to an array of mine. i want to have this string "b" that holds the letters (not an array. i do NOT want an array for that). oh, screw it, i cant explain this good. heres my code.

for (i = 0; i < arr.length; i++) {
a = arr[i].split("");
trace("A: " + a);
ls[0] = a.slice(0, 1);
ls[1] = a.slice(-1, a.length);
trace("Letter saver: " + ls);
a.splice(1, a.length-1)
a[a.length] = undefined;
for (j = 0; j < a.length; j++) {
rand = random(a.length);
if (a[rand] != undefined) {
b += a[rand];
trace("B: " + b);
a.splice(rand, 1)
rand = random(a.length);
} else {
rand = random(a.length);

quite a bit of code. basically, i have 3 arrays, "ls", "a", and "arr". the "arr" array just holds the words (split by spaces), "a" splits it for each character, "ls" holds the first and last letters of each word, and "b" is just a string. after i splice up "a" (i deleted the first and last letters in the array), i want to have this random variable be set to the array, and then have the number for the array (as set by "rand") be added to the string, and then deleted.

sorry if thats confusing. basically, i need to have that particular entry in the "a" array (once it was added to the string) to be deleted. i would assume i would use splice, but im not sure how to use it in this case. it could be any entry in the array, and it could have any number of entries.


snyggys

Response to A S: Arrays 2005-12-06 04:41:11


At 12/5/05 06:41 PM, 2k_ChristmasIzKool wrote: more array questions. i have this variable "rand" that is set to a random number according to an array of mine. i want to have this string "b" that holds the letters (not an array. i do NOT want an array for that). oh, screw it, i cant explain this good. heres my code.

for (i = 0; i < arr.length; i++) {
a = arr[i].split("");
trace("A: " + a);
ls[0] = a.slice(0, 1);
ls[1] = a.slice(-1, a.length);
trace("Letter saver: " + ls);
a.splice(1, a.length-1)
a[a.length] = undefined;
for (j = 0; j < a.length; j++) {
rand = random(a.length);
if (a[rand] != undefined) {
b += a[rand];
trace("B: " + b);
a.splice(rand, 1)
rand = random(a.length);
} else {
rand = random(a.length);

Double-posting, huh? Well, in the other thread is the right answer.
The point is, didn't you notice that doing that for with i<Array.length is not the most intelligent of ideas when it comes to cut an array. Better use a *decreasing loop* instead. (i=Array.length-1; --i)

Response to A S: Arrays 2006-01-07 21:30:01


array.toString and array.concat are a couple Array Methods I do not think you have included. They are quite simple - toString changes the variables, numbers, etc. into a string, then adds all the "elements," if you will, together seperated by commas. A similar method, join, does nearly the same - instead of being seperated by commas, however, the "elements" are seperated by a character of your choice, as typed in the arguments of the method. i.e.

myArray = new Array();
for(i=0; i<5; i++){
myArray[i] = i;
}
trace(myArray.join(" - "));

The output, of course, would be 1 - 2 - 3 - 4 - 5.

concat concatenates arrays together, the second array being the argument of the first. i.e.

myArray2 = new Array();
myArray3 = new Array();
for(i=0; i<5; i++){
myArray2[i] = i;
myArray3[i] = i;
}
trace(myArray2.concat(myArray3));

The length property of an array adds 1 to the amount of "elements" that are in an array - for example:

myArray4 = new Array;
myArray4[0] = 1000;
myArray4[6] = 10000;
trace(myArray4.length);

The output in this example would be 7, even though it appears as though it should read as 2. This is because the length property does not actually count each "element" in an array, but simply adds 1 to the last (because the contents of arrays start at the number 0, Flash adds 1 so that the number displayed is the number of elements as a normal person would count). In other words, you cannot always trust length to display correct results.

Response to A S: Arrays 2006-05-08 19:50:46


Arrgh My ears are bleading....

Could someone write a simple program using arrays, and comment so i know what the hell Im doing. I have gone over and over the arrays and I just cant get it to work.

I need an array that:

Input box
when button clicked Info goes from input box to 3 different text boxes
Then just to test it a button when clicked adds to the text boxes ( i can do)
Then a final button that transfers the textboxes back into the input box in the same order.

I really need this code but more importantly need this code in a .fla

Response to A S: Arrays 2006-08-21 01:12:48


This confuses me. I don't want to sound noobish, but I will, so the faint of heart need not read this.

Not sure If I missed somthing in there, but arras seem like a more confusing, and more advanced version of simple variable editing. Could anyone explain to me the purpose of arrays, and what use they could be put to?

Response to A S: Arrays 2007-02-19 18:26:52


How do I read values from an array within an array? For example, if my array was:

_root.ARRAY = new Array(new Array( 1, 2, 3),new Array( 4, 5, 6),new Array( 7, 8, 9));

How would I get the variable at '8'?

if you see what I mean

Thanks in advance.


...

BBS Signature

Response to A S: Arrays 2007-02-19 19:03:17


At 2/19/07 06:26 PM, edit-undo wrote: How do I read values from an array within an array? For example, if my array was:

_root.ARRAY = new Array(new Array( 1, 2, 3),new Array( 4, 5, 6),new Array( 7, 8, 9));

How would I get the variable at '8'?

if you see what I mean
Thanks in advance.

trace(_root.ARRAY[2][2]);

the first number in brackets is the array that you want to get the variable from. the 2nd [2] is the entry in that array.


snyggys

Response to A S: Arrays 2007-02-19 19:10:01


At 2/19/07 07:03 PM, Rammer wrote:
At 2/19/07 06:26 PM, edit-undo wrote:
trace(_root.ARRAY[2][2]);

the first number in brackets is the array that you want to get the variable from. the 2nd [2] is the entry in that array.

Actually, it'd be _root.ARRAY[2][1]; - array values always start at 0, as I'm sure you know

Also, @edit-undo - you can declare your array a lot more simply as

_root.ARRAY = [[1,2,3],[4,5,6],[7,8,9]];


- - Flash - Music - Images - -

BBS Signature

Response to A S: Arrays 2007-02-19 20:13:31


At 2/19/07 07:10 PM, Denvish wrote: Actually, it'd be _root.ARRAY[2][1]; - array values always start at 0, as I'm sure you know

whoops, heh. not sure why i did that.


snyggys

Response to A S: Arrays 2007-02-20 06:27:09


Thanks, Denvish and Rammer :)


...

BBS Signature

Response to A S: Arrays 2007-02-20 11:20:23


At 2/19/07 07:10 PM, Denvish wrote: Also, @edit-undo - you can declare your array a lot more simply as

tsk, denny, tsk. not encouraging strong datatyping is terrible. :P
those habits wont get you very far when you pick up AS3, thats for sure...


BBS Signature

Response to A S: Arrays 2007-03-12 13:34:25


Something I just found out, not sure if it's been mentioned or if it's useful to anyone:
.length variable
This can be used like a normal variable. If you decrease it, values will be removed from the end of the array to make the array the right length. If you increase it, undefined values will be added to the end of the array to compensate. If the .length is not an integer, it will be rounded down to make the array that length. However, the .length will still be a non-integer, i.e it's actual value doesnt changed, just it's percieved value.


...

BBS Signature

Response to A S: Arrays 2008-04-13 11:17:57


Once you've stored an array, how do you use one? Would you use it in the same way as a normal var for example?


"just because idiots are great in number does not mean they're not idiots" - alicetheDroog

The Atheist Army|English Gentleman's Club

Sig censored by: SevenSeize

BBS Signature

Response to A S: Arrays 2008-07-10 09:12:57


Heres a really useful one:

Array.Find

Array.prototype.Find=function(Element){ 
for(var i=0; i<this.length;i++){
	if(Element == this[i]){
		return i;
	}
}
}

USE:

myArray = new Array("a","b","c","d");

trace(myArray.Find("d"));

That returns the value 3.

trace(myArray.Find("e"));

returns undefined.

Useful, no?


BBS Signature

Response to A S: Arrays 2008-07-10 09:26:27


2 more search functions:

myArray = new Array("a","b","c","d","d","f","d");

Array.FindLast

Array.prototype.FindLast=function(Element){ 
for(var i=this.length; i>0;i--){
	if(Element == this[i]){
		return i;
	}
}
}

USE:

trace(myArray.FindLast("d"))

returns 6.

Array.FindAll

Array.prototype.FindAll=function(Element):Array{ 
list = new Array();
for(var i=0; i<this.length;i++){
	if(Element == this[i]){
		list.push(i);
	}
}
return list;
}

USE:

trace(myArray.FindAll("d"));

returns 3,4,6.

Pretty useful commands, especially if you're handling a lot of data


BBS Signature

Response to A S: Arrays 2008-07-10 11:18:03


Sorry for the triple post, but heres another semi-useful one:

myArray = new Array("a", "b", "c", "d");

Array.swapValue

Array.prototype.swapValue = function(Swapthis, Withthis) {
neo = new Array();
	for (i=0; i<this.length; i++) {
		if (i != Swapthis && i != Withthis) {
			neo.push(this[i]);
		} else if (i == Swapthis) {
			neo.push(this[Withthis]);
		} else if (i == Withthis) {
			neo.push(this[Swapthis]);
		}
	}
	for(e=0; e<neo.length; e++){
		this[e] = (neo[e]);
	}
};

USE:

myArray.swapValue(0, 2);
trace(myArray);

Returns c,b,a,d.

Epic.


BBS Signature

Response to A S: Arrays 2008-10-29 19:56:57


Wow, thanks, arrays are hard to get, but this made it easy!

<3