drexle
08-24-2007, 04:52 PM
Hello,
Have any of you had any moments where you realize or discover the answer to something that has eluded you for a long time and in retrospect it seems so simple that it makes you slap your forehead? I just had a total *d'oh!* moment this morning as I was reading about flash and hashes. I have long wondered why there wasn't a hash variable type in actionscript, but since I was never really a programmer before getting into AS I didn't really miss having access to them, and have been working without the concept of associative arrays in my coding.
I just read however, about how you can use Object (or even an Array) as though it were a hash by doing this:
var a:Object = new Object(); // or Array, it really doesn't matter
a["Mon"]="Monday";
a.Teu = "Teusday";
trace(a[0]+" "+a[1]);
trace(a.Mon+" "+a.Teu);
trace(a["Mon"]+" "+a["Teu"]);
What kills me is that I really should have known this. I've done similar things to objects in the past without really realizing that I was essentially making a hash. What also kills me is that very recently I was building code that checks the typeof() variable found inside of an array location, and I wanted several "if else" branches to handle Strings, Numbers, and Arrays... well when I tried to detect for typeof()=="Array" I kept getting error. The reason? Flash considers Arrays to be typeof()=="Object".
Heh. Needless to say, I feel silly right now. Anybody else have moments like these lately?
Have any of you had any moments where you realize or discover the answer to something that has eluded you for a long time and in retrospect it seems so simple that it makes you slap your forehead? I just had a total *d'oh!* moment this morning as I was reading about flash and hashes. I have long wondered why there wasn't a hash variable type in actionscript, but since I was never really a programmer before getting into AS I didn't really miss having access to them, and have been working without the concept of associative arrays in my coding.
I just read however, about how you can use Object (or even an Array) as though it were a hash by doing this:
var a:Object = new Object(); // or Array, it really doesn't matter
a["Mon"]="Monday";
a.Teu = "Teusday";
trace(a[0]+" "+a[1]);
trace(a.Mon+" "+a.Teu);
trace(a["Mon"]+" "+a["Teu"]);
What kills me is that I really should have known this. I've done similar things to objects in the past without really realizing that I was essentially making a hash. What also kills me is that very recently I was building code that checks the typeof() variable found inside of an array location, and I wanted several "if else" branches to handle Strings, Numbers, and Arrays... well when I tried to detect for typeof()=="Array" I kept getting error. The reason? Flash considers Arrays to be typeof()=="Object".
Heh. Needless to say, I feel silly right now. Anybody else have moments like these lately?