We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
array help (Read 715 times)
array help
Dec 7th, 2005, 2:14am
 
I'm not very good with arrays, my head seems to melt whenever more than one dimension is involved. can anyone explain how I need to do this - its very simple i know.

I want to record multiple start and end frames while analysing audio.

essentially I want this information stored

clip     start frame      end frame
01       100                 150
02       174                 184
03       etc                   etc

what's the best way to do this?

Cheers
n

Re: array help
Reply #1 - Dec 7th, 2005, 5:35am
 
You of course don't need to work with multidimensional arrays.
But if you'd like to...

if you know how many clips you've got before hand:

int clips = n; //obviously replace n with a number
int[][]audioData = new int[3][clips];

for (int i =0; i< clips; i++) {
 audioData [0][i] = clip id;
 audioData [1][i] = start frame;
 audioData [2][i] = end frame;
}

if you don't know how many clips before hand:

int[][]audioData = new int[3][0]

while (incoming data) {
 audioData[0] = append (audioData[0], clip id);
 audioData[1] = append (audioData[1], start frame);
 audioData[2] = append (audioData[2], end frame);
}
Re: array help
Reply #2 - Dec 7th, 2005, 11:57am
 
excellent, that makes sense.

As you rightly guessed, I don't know how many clips i will have!

cheers for the help.

Re: array help
Reply #3 - Dec 7th, 2005, 12:38pm
 
That's all working well -

Code:

audioData[0] = append (audioData[0], clip);
audioData[1] = append (audioData[1], frameOnData);
audioData[2] = append (audioData[2], frameOffData);


and I've checked that its putting in the correct data

However, I now want to save this data to a text file, but the following code isn't working. Is it possible to save multi-dimensional data to a text file this easily?

Code:

void keyPressed() {
if (key == ' ') {
println("saving");
saveBytes("numbers.txt", audioData);
}
}
Re: array help
Reply #4 - Dec 7th, 2005, 12:55pm
 
i think this should do it in your case


Code:

usage: saveString("numbers.txt",joinArrays(audioData[0],audioData[1],audioData[2]," "));

String[] joinArrays(int[] a, int[] b, int[] c, String spacer)
{
// arrays has to be of equal lengths
if(a.length != b.length || b.length != c.length || c.length != a.length) return new String[0];
String[] ret = new String[a.length];
for(int i = 0; i < a.length; i++)
ret[i] = ""+a[i]+spacer+b[i]+spacer+c[i];
return ret;
}
Page Index Toggle Pages: 1