electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
Associative Arrays: how to detect undefined?
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [9 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
diskonext



Joined: Aug 26, 2004
Posts: 307
Location: London, UK

PostPosted: Mon Sep 15, 2008 8:30 am    Post subject: Associative Arrays: how to detect undefined? Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
renderful



Joined: Apr 18, 2007
Posts: 29
Location: Boulder, Co

PostPosted: Mon Sep 15, 2008 9:43 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 3739
Location: Sweden
Audio files: 277
G2 patch files: 96

PostPosted: Mon Sep 15, 2008 12:41 pm    Post subject: Reply with quote  Mark this post and the followings unread

Also, arrays of non-primitives will return null if using an index that doesn't reference a set value (check the end of the associative arrays section in the manual here: http://chuck.cs.princeton.edu/doc/language/array.html#assoc), so if you feel the need to know if an index has been used or not, encapsulate your value in a class.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
renderful



Joined: Apr 18, 2007
Posts: 29
Location: Boulder, Co

PostPosted: Mon Sep 15, 2008 6:40 pm    Post subject: Reply with quote  Mark this post and the followings unread

array["key'] == 0 works as well
Back to top
View user's profile Send private message
diskonext



Joined: Aug 26, 2004
Posts: 307
Location: London, UK

PostPosted: Tue Sep 16, 2008 6:02 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
renderful



Joined: Apr 18, 2007
Posts: 29
Location: Boulder, Co

PostPosted: Tue Sep 16, 2008 8:02 am    Post subject: Reply with quote  Mark this post and the followings unread

Ah, I understand now.
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 3739
Location: Sweden
Audio files: 277
G2 patch files: 96

PostPosted: Tue Sep 16, 2008 10:52 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
diskonext



Joined: Aug 26, 2004
Posts: 307
Location: London, UK

PostPosted: Tue Sep 16, 2008 11:02 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
cbit



Joined: Dec 01, 2005
Posts: 35

PostPosted: Fri Nov 06, 2009 5:29 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [9 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
e-m mkii

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use