1

I have a string I got from a text file and I want to separate it from commas, and set each item in an array. I have a general idea on how to code this, but I want to make sure there is not any easier way do this. Is there a function to do this? I want something similar to split() in python, to use in a CAPL.

Before

char dataString[200] = "MAX_VOLT,  MAX_CURR,  MAX_TIME,  TEMP,  A0_MAX";

After

char dataArray[5][50] = {"MAX_VOLT","MAX_CURR","MAX_TIME","TEMP","A0_MAX"};
PySerial Killer
  • 428
  • 1
  • 9
  • 26

3 Answers3

1

I implemented a minimal CAPL script to test my idea:

variables {
  char dataString[200] = "MAX_VOLT,  MAX_CURR,  MAX_TIME,  TEMP,  A0_MAX";
  long pos1, pos2, pos3, pos4, pos5;
  char dataArray[5][50];
}

on start {
  int i;

  pos1 = strstr(dataString, ",");
  pos2 = strstr_off(dataString, pos1+1, ",");
  pos3 = strstr_off(dataString, pos2+1, ",");
  pos4 = strstr_off(dataString, pos3+1, ",");
  pos5 = strlen(dataString);
  write("pos1 = %d, pos2 = %d, pos3 = %d, pos4 = %d, pos5 = %d", pos1, pos2, pos3, pos4, strlen(dataString));

  // get first string 
  for (i = 0; i < pos1; i++) {
    dataArray[0][i] = dataString[i];
  }
  // get second string 
  for (i = 0 ; i < pos2 - pos1 - 3; i++) {
    dataArray[1][i] = dataString[i + pos1 + 3];
  }
  // get third string 
  for (i = 0 ; i < pos3 - pos2 - 3; i++) {
    dataArray[2][i] = dataString[i + pos2 + 3];
  }
  // get fourth string 
  for (i = 0 ; i < pos4 - pos3 - 3; i++) {
    dataArray[3][i] = dataString[i + pos3 + 3];
  }
  // get fifth string 
  for (i = 0 ; i < pos5 - pos4 - 3; i++) {
    dataArray[4][i] = dataString[i + pos4 + 3];
  }

  write("%s", dataArray[0]);
  write("%s", dataArray[1]);
  write("%s", dataArray[2]);
  write("%s", dataArray[3]);
  write("%s", dataArray[4]);
}

output:

CAPL    pos1 = 8, pos2 = 19, pos3 = 30, pos4 = 37, pos5 = 46
CAPL    MAX_VOLT
CAPL    MAX_CURR
CAPL    MAX_TIME
CAPL    TEMP
CAPL    A0_MAX
BruceWayne
  • 116
  • 5
0

This is just an idea, which I did'nt test. First i would get the positions of all the commas in your dataString[200]. Use the function strstr(char s1[], char s2[]); to get the position of your first comma in dataString. Use the first comma position as an input parameter offset to get the following comma positions using the strstr_off(char s1[], long offset, char s2[]);. Same for remaining commas.

Then i would use for loops using the comma positions to copy the chars from dataString to dataArray. If you use the same dataArray within your CAPL script, make sure to empty the strings in dataArray when dealing with strings of different lengths and refilling dataArray with new strings. Hope this helps.

BruceWayne
  • 116
  • 5
0

A function doesn't seem to exist to easily split a string in CAPL similar to Python. I wrote this to achieve something similar.

variables
{
  char dataString[200] = "MAX_VOLT,MAX_CURR,MAX_TIME,TEMP,A0_MAX";
  char dataArray[10][50];
}

on start
{
  split_string(dataString, ",");
  write("%s", dataArray[0]);
  write("%s", dataArray[1]);
  write("%s", dataArray[2]);
  write("%s", dataArray[3]);
  write("%s", dataArray[4]);
}

int split_string(char string[], char delim[])
{
  int start_pos, end_pos;
  int i, j;
  int num_strings;
  
  start_pos = 0;
  end_pos = 0;
  num_strings = 0;
  
  while (start_pos < strlen(string))
  {
    j = 0;
    end_pos = strstr_off(string, start_pos, delim);
    if (end_pos == -1)
    {
      end_pos = strlen(string);
    }
    for (i=start_pos; i<end_pos; i++)
    {
      dataArray[num_strings][j++] = string[i];
    }
    dataArray[num_strings][j] = '\0';
    num_strings++;
    start_pos = end_pos+strlen(delim);
  }
  
  return num_strings;
}

It doesn't remove spaces but that can probably easily be added by using str_replace.