View Full Version : AS3 Array anomoly!!??? is this for real?
-Z-
January 2nd, 2007, 12:30 AM
Whao... I just tried something and it didn't mess up...
I made an
myarray[-4_4] = something;
Is this for real??? Did I just happen apon something that is gona break later?? Or do arrays now allow negatives?
This is great, if so, I wish I could hug them for this... but since I can't...
*hugs everyone*
TheCanadian
January 2nd, 2007, 12:44 AM
Since dynamic objects can have any property names you want, arrays have always allowed negative indexes.
The only problem is that the array methods deal only with the set of whole numbers (non-negative integers), therefore all of your array methods will fail to recognize your negative indexes.
var a:Array = new Array();
a[-1] = "something";
trace(a[-1]); //something
trace(a); //
trace(a.length); //0
Dazzer
January 2nd, 2007, 07:25 AM
charming. lol
-Z-
January 2nd, 2007, 08:40 AM
Hrmmmm pft... what about the -4_4 because that's not really an integer, it's more like a string because of the _ in between the 4's.... I will see if that is counted...
stringy
January 2nd, 2007, 09:17 AM
Hrmmmm pft... what about the -4_4 because that's not really an integer, it's more like a string because of the _ in between the 4's.... I will see if that is counted...
i get an error in all 3 actionscript versions with your code but will work as a string -just adding a property to a dynamic object as mentioned by TC
var aa:Array = new Array();
aa["-4_4"]="something"
for (var prop in aa){
trace(prop+" "+aa[prop])
}
-Z-
January 2nd, 2007, 09:23 AM
i just copied what you had and it worked...
I didn't actually write out AS up there.... it was just an abbreviation of what I was doing...
I'm gona test some, if i can't do -4_4 and i can to tile-4_4 then i'm just as happy with that
Dazzer
January 2nd, 2007, 09:24 AM
question
what's the difference between using array string indices, object properties, and dictionary?
all strings.
-Z-
January 2nd, 2007, 09:35 AM
i found that no matter what, if the array is a string... it won't show the length :( so it doesn't really matter, i'm gona have to use it anyways... that stinks
slight correction.. not a string .. but if it is.... aa['string'] = 1; then it won't show the length
I already know of a work around for my problem with my game though so it's ok
stringy
January 2nd, 2007, 10:01 AM
i found that no matter what, if the array is a string... it won't show the length :( so it doesn't really matter, i'm gona have to use it anyways... that stinks
slight correction.. not a string .. but if it is.... aa['string'] = 1; then it won't show the length
I already know of a work around for my problem with my game though so it's ok
objects do not have a "length" property.you could find the number of properties added like this
var aa:Array = new Array();
aa["-4_4"]="something"
aa["-5_4"]="somethingelse"
var i=0
for (var prop in aa){
trace(prop+" "+i+" "+aa[prop])
i++
}
trace(i)
TheCanadian
January 2nd, 2007, 11:22 AM
question
what's the difference between using array string indices, object properties, and dictionary?
all strings.
Arrays are exactly the same as Objects, they just have extra methods so that you can maniplate the data more easily, as a list. Arrays can have an property name you want, just non-numeric properties won't be recognized by the various methods of the class.
Dictionaries are used to create lists, but instead of numerical or string indexes they use the memory signature of the object which allows them to use actual objects as the keys. Example:
import flash.utils.Dictionary;
var key1:Object = new Object();
var key2:Object = new Object();
var d:Dictionary = new Dictionary();
var o:Object = new Array();
d[key1] = "hello";
d[key2] = "world";
o[key1] = "hello";
o[key2] = "world";
trace(d[key1]); //hello
trace(d[key2]); //world
trace(o[key1]); //world
trace(o[key2]); //world
//both output since the keys are added as the return of the objects toString method, which means
//that the key being used for both objects is [object Object]
trace(o["[object Object]"]); //world
jjcorreia
January 2nd, 2007, 02:32 PM
I dont know why you would even want to do that. If you want to use not integers just use a generic or custom object.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.