cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
jthi
Super User

Associative arrays and removal of keys with value same as default value

Just noticed new behaviour (for me) that when using Associative Arrays() and the key you have, has the same value as default value, the key might disappear. I noticed this because I was using Associative Array to collect different status messages (default value was set to 0) and then used that associative array to build user interface after collecting statuses.

 

Create Associative Arrays does have following note so this is expected behaviour:

Note: If a key’s value is the default value, then the key is dropped because any key will return the default value.

One example where that Note doesn't seem to hold truth:

 

Names Default To Here(1);
aa = [=> 0];
aa["a"] += 0;
show(aa); // aa = ["a" => 0, => 0];
aa["a"] = 0;
show(aa); // aa = [=> 0];

 

Any ideas why it works like this? Due to this behaviour (keys getting deleted) I will most likely give up with default values in Associative Array (for now) so I can have same behaviour in all of my scripts and just handle the errors caused by keys which are not found from the Associative Array.

 

Edit:

I would prefer the keys not being removed at all, when they have same value as default value. This is most likely so deeply built into JMP for some good reason (optimizations?) that I suspect it won't be changed

-Jarmo
2 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales
Super User

Re: Associative arrays and removal of keys with value same as default value

No idea. It isn't the behavior I'd expect either.

aa = [=> 0];
aa["a"] += 0;
show(aa); // aa = ["a" => 0, => 0];
aa["a"] -= aa["a"] ;
show(aa); // aa =  ["a" => 0, => 0]; ! good ?

My normal usage is similar, except I'd only use +=1 to count occurrences, so I'd never see it.

Also, don't use a { }, [ ], or [ => ] for an initial value. It does not work the way you expect, though it may shed some light on the issue (it changes the default value without adding an element.)

 

For future-proofing, in case this gets changed for the better, I'd use explicit <<remove("a") if that's what I want, and if I want to modify a default value, I'd use explicit <<getDefault and <<setDefault.

 

 

Craige

View solution in original post

jthi
Super User

Re: Associative arrays and removal of keys with value same as default value

I did get quite a few answers regarding this from JMP support, I'll try to write some sort of summary of those. 

 

My question to support

If possible I would like to understand why JMP's Associative Arrays work like they do with default values, as in they delete keys if they have same value as default value. Also why it doesn't behave like this always? I have posted about this to community https://community.jmp.com/t5/Discussions/Associative-arrays-and-removal-of-keys-with-value-same-as/t...
Test script with behaviour which doesn't work as intended (in my opinion):

Names Default To Here(1);
aa = [=> 0];
aa["a"] += 0;
show(aa); // aa = ["a" => 0, => 0];
aa["a"] = 0;
show(aa); // aa = [=>0];

 

Summary of answers from support:

I just wanted to share with you the answer I received from R&D: this behavior was implemented in associative arrays as part of modeling sets and set operations with the data structure. The particular inconsistency that you highlighted will be addressed in a future release (JMP-776).

 

I also provided them "example" which was very close to the situation in which I noticed the problem and they provided me with this script (I had already modified/solved this other way, but I'll share this also):

Below is slightly modified version of your example that does not use a default value for the associative array, but handles using a key that does not exist in the array.  In this example, the algorithm ensures that the icons are always aligned with the proper description.

 

Names Default To Here(1);
 
//init aa
aa_status = Associative Array();
aa_status["Connection Check"] = "0";
aa_status["Folder found"] = "";
aa_status["To Delete"] = "1";
 
New Window("test",
       Table Box(
              des_ref = String Col Box("Description", aa_status << get keys),
              cb_ref = Col Box("Status", {}),
       )
);
 
//aa_status["Folder found"] = "0";
aa_status << Remove Item("To Delete");
For Each({val}, des_ref << Get,
       If(Try(aa_status[val] == "0", 1),
              cb_ref << Set({Icon Box("Excluded")});
       ,
              cb_ref << Set({Icon Box("CheckCircle")});
       )
);
 

 

 

 

-Jarmo

View solution in original post

2 REPLIES 2
Craige_Hales
Super User

Re: Associative arrays and removal of keys with value same as default value

No idea. It isn't the behavior I'd expect either.

aa = [=> 0];
aa["a"] += 0;
show(aa); // aa = ["a" => 0, => 0];
aa["a"] -= aa["a"] ;
show(aa); // aa =  ["a" => 0, => 0]; ! good ?

My normal usage is similar, except I'd only use +=1 to count occurrences, so I'd never see it.

Also, don't use a { }, [ ], or [ => ] for an initial value. It does not work the way you expect, though it may shed some light on the issue (it changes the default value without adding an element.)

 

For future-proofing, in case this gets changed for the better, I'd use explicit <<remove("a") if that's what I want, and if I want to modify a default value, I'd use explicit <<getDefault and <<setDefault.

 

 

Craige
jthi
Super User

Re: Associative arrays and removal of keys with value same as default value

I did get quite a few answers regarding this from JMP support, I'll try to write some sort of summary of those. 

 

My question to support

If possible I would like to understand why JMP's Associative Arrays work like they do with default values, as in they delete keys if they have same value as default value. Also why it doesn't behave like this always? I have posted about this to community https://community.jmp.com/t5/Discussions/Associative-arrays-and-removal-of-keys-with-value-same-as/t...
Test script with behaviour which doesn't work as intended (in my opinion):

Names Default To Here(1);
aa = [=> 0];
aa["a"] += 0;
show(aa); // aa = ["a" => 0, => 0];
aa["a"] = 0;
show(aa); // aa = [=>0];

 

Summary of answers from support:

I just wanted to share with you the answer I received from R&D: this behavior was implemented in associative arrays as part of modeling sets and set operations with the data structure. The particular inconsistency that you highlighted will be addressed in a future release (JMP-776).

 

I also provided them "example" which was very close to the situation in which I noticed the problem and they provided me with this script (I had already modified/solved this other way, but I'll share this also):

Below is slightly modified version of your example that does not use a default value for the associative array, but handles using a key that does not exist in the array.  In this example, the algorithm ensures that the icons are always aligned with the proper description.

 

Names Default To Here(1);
 
//init aa
aa_status = Associative Array();
aa_status["Connection Check"] = "0";
aa_status["Folder found"] = "";
aa_status["To Delete"] = "1";
 
New Window("test",
       Table Box(
              des_ref = String Col Box("Description", aa_status << get keys),
              cb_ref = Col Box("Status", {}),
       )
);
 
//aa_status["Folder found"] = "0";
aa_status << Remove Item("To Delete");
For Each({val}, des_ref << Get,
       If(Try(aa_status[val] == "0", 1),
              cb_ref << Set({Icon Box("Excluded")});
       ,
              cb_ref << Set({Icon Box("CheckCircle")});
       )
);
 

 

 

 

-Jarmo