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 
 Forum index » DIY Hardware and Software » ChucK programming language
modifying array sizes
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [4 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
chuckbeginner



Joined: May 26, 2008
Posts: 29
Location: Asheville NC

PostPosted: Sun Nov 16, 2008 10:31 am    Post subject: modifying array sizes
Subject description: is there a way to do it?
Reply with quote  Mark this post and the followings unread

I would like to change the size of the array after I have already filled the entries of the array, then throw data into the new empty slots. Is this possible?

I have been trying

array.cap() => x;

x+1 => array.cap;

When I do <<<array>>>, it gives me the right answer. But when I try to assign a value to the slot I think I just created , it gives me an ArrayOutOfBounds message.

Is this possible? Do any of you know the correct syntax/method?
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Sun Nov 16, 2008 12:43 pm    Post subject: Reply with quote  Mark this post and the followings unread

Sure.

From /examples/array/dynamic.ck;

Code:
// instantiate int array
float argh[0];

// print out relevant info
<<< "array size:", argh.size() >>>;

// append items (array should grow dynamically as needed)
argh << 3.0 << 4 << 5;

// print out relevant info
<<< "array size:", argh.size() >>>;

// print out elements
<<< "contents:", argh[0], argh[1], argh[2] >>>;

// pop element
argh.popBack();

// print out relevant info
<<< "array size:", argh.size() >>>;


The "<<" operator appends a array with a element. That is; it increases the size by one and sets this value/object as the last one.

Also useful;
Code:


int array[3];

<<<"size =", array.cap()>>>;

(array.cap() + 1) => array.size;

<<<"now size =", array.cap()>>>;

12 => array[3];

<<<"printing array's contents", "">>>;
for(int n; n< array.cap(); n++) <<<array[n]>>>;


Writing to .cap used to work (though at the expense of flushing the data) but it seems it doesn't any more; .size() is more useful now.

Not that at the moment there is a bug for arrays of non-primitive types that causes the elements to be initiated non-properly if .size() is used and you should use this instead;
Code:

my_array << new my_object;


Hopefully this will soon be fixed.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
chuckbeginner



Joined: May 26, 2008
Posts: 29
Location: Asheville NC

PostPosted: Mon Nov 17, 2008 1:45 pm    Post subject: Reply with quote  Mark this post and the followings unread

Thanks for the help!

What version was this functionality implemented in? I don't see that example file in my examples folder...
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Nov 17, 2008 6:00 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hmmmm, I think that's 1.2.1.2 so the latest. I unless you have good reasons not to I'd stick to using the latest version and using the list(s) to make sure you are notified when one gets released.
_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [4 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