-
Hashtable/dictionary?
I am searching for the best way to store objects. I have a "User"-class with some information like this:
User = function () {
this.conn_id;
this.nickname;
}
I would like to store this in some kind of hashtable/dictionary for easy access. I would like to have the variable "conn_id" as key to the hashtable. Is there any built in functions in flash mx to retrieve and delete objects in a hashtable based on keys? Somthing like this:
u = new User();
u.conn_id = "1";
u.nickname = "Joe";
hashTable.addItem(u.conn_id,u);
hashTable.removeItem("1");
Thanks / Henrik
Last edited by mrdance; 12-19-2003 at 08:02 AM.
-
Hi there,
It's actually much simpler in Actionscript: Hashes (they're called "associative arrays" here) and objects are the same thing:
someObj.prop == someObj["prop"]
I suppose, when you come from another programming language this is quite a surprise. But a sweet one, I think.
pecoes
-
ok, I see that there is a lot of ways to do this but let's see I do this:
User = function (conn_id,nickname) {
this.conn_id = conn_id;
this.nickname = nickname;
}
u = new User("123","Joe");
users = new Array();
users[u.conn_id] = u;
Now I can access it using users[u.conn_id].nickname for example. The problem I face is when I want to count number of objects in the array. My variable conn_id can be anything between 1 and 100000. Let's say I first add a conn_id with value 10 and then vanlue 100000, then it seems to allocate an array from 0 to 100000. It seems unesseary to loop through all these just to find out that I only have 2 objects. Other languages can solve this with a dictionary object which includes functions as AddItem(id), removeItem(id). How can I solve this the best way? Dataset? Or is looping through the array fast enough?
thanks / Henrik
-
The problem isn't that you would create bloated data structures. This:
a = new Array();
a[9999] = 'ten thousand';
a[10] = 'eleven';
creates an array/hash/object with just two entries.
The problem is the length property:
trace(a.length); // yields 10000
The length-property, does not seem to return the array's length anyway, but the value of its biggest numerical key plus one.
I can ony think of this nasty hack, I'm afraid:
Array.prototype.count = function () {
var i, c = 0;
for (i in this) c++;
return --c; // i=='count' counts too ...
}
trace(a.count()); // yields 2
oh, btw: Is it Python?
Last edited by pecoes; 12-19-2003 at 10:20 AM.
-
nice hack, thanks!
What about listing all two objects with looping through the array (from 1 to 10000). (I want to find only the defined objects in the array.) Do you have any tricks for that?
thanks / Henrik
-
Same trick: the "for ... in" loop will only return keys for defined values.
for (i in a) trace(a[i]);
-
Thanks! I hade some strange experiences with this code:
users = new Array();
u = new User("132","Rai");
u.test="test342";
users[u.conn_id] = u;
u = new User("132324","JP");
u.test="test342";
users[u.conn_id] = u;
// get defined objects in array
Array.prototype.getNickname = function () {
var i, c = 0;
for (i in this) {
if (users[i].nickname != undefined) {
trace(users[i].nickname);
}
}
}
// count instances of class in array
Array.prototype.count = function () {
var i, c = 0;
for (i in this) c++;
return --c; // i=='count' counts too ...
}
trace("Count:" + users.count()); // now yields 3???
users.getNickname();
After adding the the prototype getNickname count now yields 3! Why? Another thing, the new "for (i in this)" in getNickname returns undefined values. If I trace users[i] it returns:
[type Function]
[type Function]
[object Object]
[object Object]
Why? Thank you again for all your help! / Henrik
-
The reason why count returns 3, is that it also counts "getNickName" as a content of "users" - functions are data too. You might want to build in some additional type-check:
PHP Code:
Array.prototype.getNickname = function () {
var i, c = 0;
for (i in this)
if (typeof i == 'number')
trace(this[i].nickname);
}
If you really want to get eccentric, there's an undocumented function called "ASSetPropFlags", that allows you to hide properties from "for .. in" loops:
http://www.actionscript.com/referenc...tpropflags.php
Last edited by pecoes; 12-19-2003 at 01:25 PM.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|