View Full Version : You too can Associative Array Reference! (But can I?)
lrhb
September 15th, 2003, 03:24 PM
Hi there!
I'm trying to dynamically define some variables but I seem to be doing something wrong...
function placeData(color){
plinky["color"] = new Array();
plinky["color"]name = "angry";
}
plinky = new Object();
results = placeData(red);
trace(results);
trace (results) should give back "angry", but alas, I get the following message:
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 3: ';' expected
plinky["color"]name = "angry";
At the moment, I am the captain of malformating! Any assistance and advice would be greatly appreciated!
Thanks! :hat:
lrhb
claudio
September 15th, 2003, 03:32 PM
plinky["color"]name = "angry";Should readplinky["color"].name = "angry";And what are you trying to achieve? :h:
Voetsjoeba
September 15th, 2003, 03:33 PM
function placeData(clr){
plinky[clr] = new Array();
plinky[clr].name = "angry";
return plinky[clr].name
}
plinky = new Object();
results = placeData("red");
trace(results);
Explanation coming up ...
Voetsjoeba
September 15th, 2003, 03:44 PM
function placeData(color){
plinky["color"] = new Array();
plinky["color"]name = "angry";
}
plinky = new Object();
results = placeData(red);
trace(results);
Ok. Firstly, you used color as argument, which is a keyword in Flash. I changed it to clr. It's always better not to use keywords.I'll replace every color with clr from here on.
You also used plinky["clr"]. Flash will see this as plinky.clr, but not the argument clr, since you used a string ("the quotes") instead of the argument. So the correct code would be plinky[clr] instead of plinky["clr"].
plinky[clr]name should've been plinky[clr].name. That's just syntax, not much to explain. Take note though, that this line doesn't add a value to the array. It's adds a property to the object plinky[clr]. To add the value to the array, you would use plinky[clr][0] = "angry".
The function you have doesn't return a value, so you can't assign it to a variable. So to have it return the value, we use return variable. I returned the property name here, so it can be assigned to results.
You assigned placeData(red) to results. This will only work if red is a variable. placeData("red") passes along the string red, instead of the value of the variable red (which doesn't exist).
:)
lrhb
September 15th, 2003, 03:50 PM
Thank you my friends for the speedy replies! My AS had more problems than a high school Algebra textbook!
Voetsjoeba, your explanation helped greatly... I forgot that "color" was an argument. I am a non-confrontational person so I tend to ignore arguments.... :nerd:
I have learned much... thank you for your time everyone :hat:
lrhb (learning!)
Voetsjoeba
September 15th, 2003, 03:53 PM
No problem =)
senocular
September 15th, 2003, 04:16 PM
theres a pretty hefty post in the best of kirupa about associative array referencing too
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.