| Author |
Message |
diskonext

Joined: Aug 26, 2004 Posts: 306 Location: London, UK
|
Posted: Mon Sep 15, 2008 8:30 am Post subject:
Associative Arrays: how to detect undefined? |
 |
|
I love associative arrays, but in ChucK I foresee having the following problem:
| Code: |
int iArray[0];
0 => iArray["zero"];
1 => iArray["one"];
if ( iArray["zero"] == iArray["undefined"] ) {
<<< "Houston we have a problem" >>>;
}
|
Which will print "Houston we have a problem", because the value for an 'undefined' array entry is 0 in the int case (and 0.0 in the float case).
This means there is no way to detect whether it was set to 0 or whether it was *not* set. In the float case you can work around this by defining ZERO to be 0.00000001 or something like that, but in the int case it is more worrying.
I would hope for something like NULL (C style) or undef (perl style) to distinguish the case. Or perhaps (uglier) array_key_exists(iArray, "zero") i PHP style?
Or am I missing something? _________________ :wq |
|
|
Back to top
|
|
 |
renderful

Joined: Apr 18, 2007 Posts: 29 Location: Boulder, Co
|
Posted: Mon Sep 15, 2008 9:43 am Post subject:
|
 |
|
This works:
| Code: |
int iArray[0];
0 => iArray["zero"];
1 => iArray["one"];
if ( !iArray["zero"] ) {
<<< "Houston we have a problem" >>>;
}
|
The !Array["zero"] is the key. |
|
|
Back to top
|
|
 |
Antimon
Joined: Jan 18, 2005 Posts: 4145 Location: Sweden
Audio files: 371
G2 patch files: 100
|
|
|
Back to top
|
|
 |
renderful

Joined: Apr 18, 2007 Posts: 29 Location: Boulder, Co
|
Posted: Mon Sep 15, 2008 6:40 pm Post subject:
|
 |
|
| array["key'] == 0 works as well |
|
|
Back to top
|
|
 |
diskonext

Joined: Aug 26, 2004 Posts: 306 Location: London, UK
|
Posted: Tue Sep 16, 2008 6:02 am Post subject:
|
 |
|
Antimon: thank you, that would work - although I prefer not to encapsulate ints and floats for this reason alone
Renderful: not quite sure what you mean? You seem to underline the problem as I want to be able to distinguish between 0 (the value) and 0 (the undefined key) - but can not do that at the moment, unless I use Antimon's trick.
I would like to be able to throw an associative array at an instrument that has many control points (FiltAfreq, FiltAQ, OscBGain, OscBPhase &c) and leave values undefined if I don't want to change them:
I would then loop over all potential commands to find out which ones need changing - with the current mechanism I could either never use 0 as a value, or I would set many parameters to 0 if not specified in the 'command' array.
I personally think that the NULL behaviour should be used for associative arrays of all types - or have a command to check for the existence of the key. _________________ :wq |
|
|
Back to top
|
|
 |
renderful

Joined: Apr 18, 2007 Posts: 29 Location: Boulder, Co
|
Posted: Tue Sep 16, 2008 8:02 am Post subject:
|
 |
|
| Ah, I understand now. |
|
|
Back to top
|
|
 |
Antimon
Joined: Jan 18, 2005 Posts: 4145 Location: Sweden
Audio files: 371
G2 patch files: 100
|
Posted: Tue Sep 16, 2008 10:52 am Post subject:
|
 |
|
I'm not sure I agree that you should be able to get NULL from an int[] array - I feel that would go against the notion of a primitive variable which shouldn't be able to be NULL. It can be useful to have a default value of zero as well. However, I can definitely see a need to be able to retreive the set of used keys for an array so you could do something like this:
array.keys() @=> KeySet keys;
if (keys.contains("FiltAfreq")) {
array["FiltAfreq"] => filterA.freq;
}
Maybe this already exists somewhere, just not in the docs?
/Stefan _________________ Antimon's Window
@soundcloud @Flattr home - you can't explain music |
|
|
Back to top
|
|
 |
diskonext

Joined: Aug 26, 2004 Posts: 306 Location: London, UK
|
Posted: Tue Sep 16, 2008 11:02 am Post subject:
|
 |
|
Either that or a keys() function that exports an array of existing keys to be used in a for-loop.
I had some discussions here in the office and the consensus was that NULL (or any other special bit sequence indicating an undefined value) is not always a good method, especially for the native types. _________________ :wq |
|
|
Back to top
|
|
 |
cbit

Joined: Dec 01, 2005 Posts: 35
|
Posted: Fri Nov 06, 2009 5:29 am Post subject:
|
 |
|
I was looking for a way of iterating over the associative portion of an array who's keys are not known. But as I gather from this discussion, array objects don't currently provide a way of finding out what their associative keys are, that's a pity. (subscribes to thread). _________________ http://basementhum.blogspot.com |
|
|
Back to top
|
|
 |
|