I've taken liberties with some code posted here a couple of weeks ago
and modified it to accept any multidimensional hash ( assoc array ) in
the spirit of a perl hash.
I'm a beginner so any critique is welcomed or it maybe useful to someone
else.
////////////////////////////////////////////////////////////
qx.Class.define("custom.Hash",
{
extend : qx.core.Object,
construct : function()
{
this.base(arguments);
this.__values = { };
},
members :
{
//#####################################################
//#####################################################
add : function()
{
var cnt = arguments.length - 1 ;
var value = arguments[cnt];
var myobject = this.__values;
for (i = 0;i < cnt; i++) {
if (i < (cnt-1) ) {
if (myobject[arguments[i]] == undefined) {
myobject[arguments[i]]={};
}
myobject = myobject[arguments[i]];
} else {
myobject[arguments[i]]= value;
}
}
},
//#####################################################
get : function()
{
var cnt = arguments.length - 1 ;
var myobject = this.__values;
for (i = 0;i <= cnt; i++) {
if (i < cnt ) {
if (myobject[arguments[i]] != undefined) {
myobject = myobject[arguments[i]];
} else {
return undefined;
}
} else {
return myobject[arguments[i]];
}
}
},
//#####################################################
remove : function(key)
{
var cnt = arguments.length - 1 ;
var myobject = this.__values;
for (i = 0;i <= cnt; i++) {
if (i < cnt ) {
if (myobject[arguments[i]] != undefined) {
myobject = myobject[arguments[i]];
} else {
return 0;
}
} else {
return delete myobject[arguments[i]];
}
}
},
//#####################################################
sortkeys : function()
{
var cnt = arguments.length - 1 ;
var myobject = this.__values;
for (i = 0;i <= cnt; i++) {
if (myobject[arguments[i]] != undefined) {
myobject = myobject[arguments[i]];
} else {
return undefined;
}
}
var keys = [];
for(var key in myobject) {
keys.push(key);
}
return keys.sort();
},
//#####################################################
sortvalues : function()
{
var cnt = arguments.length - 1 ;
var myobject = this.__values;
for (i = 0;i <= cnt; i++) {
if (myobject[arguments[i]] != undefined) {
myobject = myobject[arguments[i]];
} else {
return undefined;
}
}
var values = [];
for(var key in myobject) {
values.push(myobject[key]);
}
return values.sort();
}
//#####################################################
}
});
////////////////////////////////////////////////////////////
// examples: (keys first , value is last item )
var hash = new custom.Hash();
hash.add("xx","cc", "vv", "xx word 1");
hash.add("xx","cc", "ff", "yy word 2");
hash.add("xx","cc", "hh", "ii word 3");
hash.add("xx","ll", "jj", "pp word 4");
hash.add("xx","ll", "mm", "ww word 5");
hash.add("aa","ll", "nn", "ss word 6");
hash.add("aa","ll", "mm", "zz word 7");
hash.add("aa","ll", "pp", "ll word 8");
hash.add("pp","ll", "pp", "ll word 8");
alert(hash.get("xx","ll", "jj"));
alert(hash.sortkeys());
alert(hash.remove("pp"));
alert(hash.sortkeys());
alert(hash.sortkeys("xx","cc"));
alert(hash.remove("xx","cc", "hh"));
alert(hash.sortkeys("xx","cc"));
alert(hash.sortkeys("xx"));
alert(hash.sortkeys("aa","ll"));
alert(hash.sortvalues("aa","ll"));
On Mon, 2010-07-19 at 08:46 -0400, Derrell Lipman wrote:
> On Mon, Jul 19, 2010 at 08:21, Fritz Zaucker <[email protected]>
> wrote:
> Hi Martin,
>
> I understand. In Perl there isn't any order in hashes
> (associative arrays)
> either:
>
> foreach my $key (keys %hash) {
> print "$key\n";
> }
>
> does not give you any particular order.
>
> Therefore you do
>
> foreach my $key (sort keys %hash) {
> print "$key\n";
> }
>
> (assuming you want alphabetical order, for numerical order
> you'd have to
> specify a sort routine, similar to what sort() in JS does).
>
> Note, the sorting is done by sorting the indices to the hash
> (called keys in
> Perl).
>
> There is no standard function or methodology for easily obtaining all
> of the keys of an object in JavaScript. You can do the following if
> you want:
>
> var sortedKeys = [ ];
> for (var key in hash)
> {
> sortedKeys.push(key);
> }
> sortedKeys.sort();
>
> Now you can iterate through the sorted keys array to access the values
> in key-sorted order:
>
> for (var i = 0; i < sortedKeys.length; i++)
> {
> console.log("The hash of " + sortedKeys[i] + " is " +
> hash[sortedKeys[i]]);
> }
>
> It's a bit messy, but for those cases where you really need it, it can
> be done. You might also consider creating a class to make this easier:
>
> qx.Class.define("custom.Hash",
> {
> extend : qx.core.Object,
>
> construct : function()
> {
> this.base(arguments);
>
> // Create an array to keep the keys for quick access or sorting
> this.__keys = [ ];
>
> // The place to keep the values, per key
> this.__values = { };
> },
>
> members :
> {
> put : function(key, value)
> {
> // Does this key already exist?
> if (this.__values[key] === undefined)
> {
> // Nope. Add its key to the key list
> this.__keys.push(key);
> }
>
> // Save the value
> this.__values[key] = value;
> },
>
> get : function(key)
> {
> // Return the value corresponding to the given key
> return this.__values[key];
> },
>
> remove : function(key)
> {
> // Remove the key from the keys array
> qx.lang.Array.remove(this.__keys, key);
>
> // Remove the member from the values map
> delete this.__values[key];
> },
>
> getSortedKeys : function()
> {
> // Make a copy of the keys so the user doesn't screw with ours
> var keys = qx.lang.Array.clone(this.__keys);
>
> // Sort the keys
> keys.sort();
>
> // Give 'em what they came for!
> return keys;
> }
> }
> });
>
> // Create a hash table
> var hash = new custom.Hash();
> hash.put("xyzzy", "magic word 1");
> hash.put("abracadabra", "magic word 2");
> hash.put("crutiatus", "magic word 3");
>
> // Retrieve the values in key-sorted order
> var sortedKeys = hash.getSortedKeys();
> for (var i = 0; i < sortedKeys.length; i++)
> {
> var key = sortedKeys[i];
> console.log("key=" + key + ", value=" + hash.get(key));
> }
>
> Derrell
>
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________ qooxdoo-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://ad.doubleclick.net/clk;226879339;13503038;l?
http://clk.atdmt.com/CRS/go/247765532/direct/01/
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel