Splitting a set of data

I have a set of data coming in to the arduino formatted generally like this: D,D,L,2,7,9,67,87,8,10 the first 3 letters have to do with instruction for the system I'm programming and the numbers and how many numbers there are, is not constant. The first number determines how many numbers there are because you need to group the rest of the data in 3 groups each containing that many numbers. So here the number is 2, so there will be 6 numbers after it and if it were 3 it would have 9 numbers after it. The first number can be anything from 1-6. I need a way to be able to separate the numbers to look like this: 2 (7,9) (67,87) (8,10) and for any value that the first number could be.

use strtok().

So:

char data[32]; // code further down assumes this contains your data

char *tmp_p = strtok( data, "," );  // This gets the first value before a comma.
int counter = atoi( tmp_p );

for( int j=0; j<3; j++ )  // always 3 sets of data? ...
{
  for( int i=0; i<counter; i++ )
  {
    // set up a group  
    tmp_p = strtok( NULL, "," );
    // do somethign with that data, I'll leave this part for you to do, but you can format with sprintf()
  }
}
char data[32]; // code further down assumes this contains your data

The part of the data after the first three values and delimiters have been removed, that is.

for( int j=0; j<3; j++ )  // always 3 sets of data? ...

No, that is not what OP said. The outer loop should run from 0 to counter (the number of sets of data). This should be the inner loop, which iterates over the number of pieces of data in a set (two, in OP’s case).

    // do somethign with that data, I'll leave this part for you to do, but you can format with sprintf()

sprintf()? OP appears to need to use the data as integers, so atoi() would be more appropriate.

PaulS:

char data[32]; // code further down assumes this contains your data

The part of the data after the first three values and delimiters have been removed, that is.

Yes, sorry, thats the data minus the letters, Would make sense to run them through strtok also prior to the loops.

The first number determines how many numbers there are because you need to group the rest of the data in 3 groups each containing that many numbers.

PaulS:

for( int j=0; j<3; j++ )  // always 3 sets of data? ...

No, that is not what OP said. The outer loop should run from 0 to counter (the number of sets of data). This should be the inner loop, which iterates over the number of pieces of data in a set (two, in OP’s case).

He specifically said 3 sets. It does not matter which way the loops are ordered, as it happens having the loops the way I have put them, allows for the parsing on each group in the inner loop.

PaulS:

    // do somethign with that data, I'll leave this part for you to do, but you can format with sprintf()

sprintf()? OP appears to need to use the data as integers, so atoi() would be more appropriate.

I need a way to be able to separate the numbers to look like this: 2 (7,9) (67,87) (8,10) and for any value that the first number could be.

Perhaps this was a bit ambiguous. I read to mean he wanted to split them in order to reconstruct a string, for whatever reason.

aels747: I have a set of data coming in to the arduino formatted generally like this: D,D,L,2,7,9,67,87,8,10

Have a look at the examples in serial input basics. The second example will probably receive your data reliably, but if you can add start- and end-markers as in the third example it will be better.

The parse example can easily be adapted to separate your data.

...R

Thank you so much for your replies. But I’m having a little trouble printing it into the serial.
Here is my code so far:

char data[32]; //example
String content = "";

void setup() {
  
  Serial.begin(9600);
}


void loop()
{
  
  char *tmp_p = strtok( data, "," );  // This gets the first value before a comma.
  Serial.println(data);
int counter = atoi( tmp_p );
Serial.println(tmp_p);
for( int j=0; j<3; j++ )  // always 3 sets of data

{
  for( int i=0; i<counter; i++ );
  {
    // set up a group 
    tmp_p = strtok( NULL, "," );
    
  }
}}

If you haven’t already been able to tell, I’m still a beginner at all this.

The parse example can easily be adapted to separate your data.

I don’t quite understand parse. Could you help me out. I’ve tried using it, but I cant seem to get it to work either.

Also, there will always be three characters before the first integer. The first two characters are always ‘D,D’, however the third character may be a ‘D’, or an ‘L’.

I have a set of data coming in to the arduino formatted generally like this: D,D,L,2,7,9,67,87,8,10

Where is this in your code?

Here is my code so far

No one anywhere suggested using the String class. Why are you?

aels747: Thank you so much for your replies. But I'm having a little trouble printing it into the serial. Here is my code so far:

Why not just include the functions from serial input basics into your program ?

...R

Something along these lines should work:

char data[32] = "D,D,L,2,7,9,67,87,8,10"; //example


void setup() {
  int sets[5][3];     // Set for 5 sets of 3
  int i, j;

  Serial.begin(9600);
  
  char *tmp_p = strtok( &data[6], "," );  // This gets the first value before a comma.
  int counter = atoi( tmp_p );

  
  Serial.println(tmp_p);
  tmp_p = strtok('\0', "," );
  while (tmp_p) {
    for(i = 0; i < counter; i++) {
      for(j = 0; j < 3; j++ ) { // always 3 sets of data
        sets[i][j] = atoi(tmp_p);
        tmp_p = strtok('\0', "," );
      }
    }
  }
  Serial.println("Results:");
  for (i = 0; i < counter; i++) {
    for (j = 0; j < 3; j++) {
      Serial.print(sets[i][j]);
      Serial.print("   ");
    }
    Serial.println();
  }
}

void loop()
{
}