electro-music.org   Dedicated to experimental electro-acoustic
and electronic music
 
 Front Page  |  Radio  |  Forum  |  Media  |  Playlists  |  Wiki
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 

Brief maintenance shutdown at 10:00 PM EST - 0400 UTC

Coming up on Radio 1 at
Twyndyllyngs live on Chez Mosc
 
 Forum index » DIY Hardware and Software » ChucK programming language
string issue
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Fri Dec 17, 2010 2:28 pm    Post subject: string issue Reply with quote  Mark this post and the followings unread

Hi,

It's possible to construct a string and pass this string as the instance name of a class?

f.e.

we have a class:

ourClass inst1,inst22;

and then access the class with a function like:

fun void test(string instanceNumber)
{
"inst"+"instanceNumber".doSomething(int whatever);
}

the idea is to access all the instances with the same function.

Every help will be much appreciated!
Back to top
View user's profile Send private message
vrachnasormora



Joined: Mar 22, 2007
Posts: 42
Location: Preveza,Greece

PostPosted: Fri Dec 17, 2010 6:20 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi Jirko,

you can build this functionality with an associative array:

Code:
class A
{
    int x;
   
    fun void print()
    {
        <<< x >>>;
    }
}


A keeper[0];

new A @=> keeper["a1"];
new A @=> keeper["a2"];

5 => keeper["a1"].x;
6 => keeper["a2"].x;

fun void test( string no )
{
    keeper["a" + no].print();
}

//testing
test( "1" );
test( "2" );


Cheers,
Andreas
Back to top
View user's profile Send private message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Sat Dec 18, 2010 5:25 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks a lot Andreas. Works perfect in my class!!
Back to top
View user's profile Send private message
vrachnasormora



Joined: Mar 22, 2007
Posts: 42
Location: Preveza,Greece

PostPosted: Sat Dec 18, 2010 10:17 am    Post subject: Reply with quote  Mark this post and the followings unread

I'm glad that it worked for you.
Just in case you would ever want to access a multi-dimensional array with
associative indices ( and IMHO there is a good reason to do this because of
the flexibility it offers when you want to construct virtual patchbays, presets'
mechanisms, etc ) there's a workaround in the syntax.So the above example
with 2 groups of 2 objects each, would become:

Code:
class A
{
    int x;
   
    fun void print()
    {
        <<< x >>>;
    }
}


A keeper[0][2];

new A[2] @=> keeper["group1"];
new A[2] @=> keeper["group2"];

5 => (keeper["group1"])[0].x; // notice the change in syntax
6 => (keeper["group1"])[1].x; 
7 => (keeper["group2"])[0].x; // use parentheses around the
8 => (keeper["group2"])[1].x; // first dimension of the array

fun void test( string no, int index )
{
    (keeper["group" + no])[index].print(); // use parantheses again here
}

//testing
test( "1", 0 );
test( "1", 1 );
test( "2", 0 );
test( "2", 1 );


This workaround was mentioned in the mailing list back in 2006, and it remains as such.
Back to top
View user's profile Send private message
jirko



Joined: Dec 15, 2009
Posts: 59

PostPosted: Tue Dec 21, 2010 5:30 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks again Andreas for clearing this matter. It's very useful to have this info here in the forum.

Cheers!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 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
Log in
Username:     Password:      Log me on automatically each visit    


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2026 by electro-music.org