Login Page - Create Account

Support Board


Date/Time: Mon, 25 Nov 2024 21:22:11 +0000



[Programming Help] - Parse a comma delimited string

View Count: 382

[2024-02-05 02:47:14]
Ticks - Posts: 183
Has anyone ever been able to parse a string and place the values into variables using SC code?
An example would be:

float FirstValue=0 ;
float SecondValue=0 ;

SCString inputString = "1,17945";
SCString delimiter = ",";

Need to parse and place the values into the variables
FirstValue= would be the 1 ;
SecondValue= would be the 17945 ;

Has anyone figured out how to do this?
Could not find any examples in the ACS_Souce folder or even on the support board.
[2024-02-06 18:18:08]
Ticks - Posts: 183
Never Mind. I figured it out and got it working perfectly.
I am now able to read a text file, parse its contents, and delete the file.

It just sucks that I had to struggle so hard to get this figured out when SC has not been able to provide any examples in the ACS_Souce folder or even on the support board.

Now to start working on the 2nd part of my project of using Ninjascript to capture data from NinjaTrader and placing that data in the file for reading and processing within SC.
Date Time Of Last Edit: 2024-02-06 19:53:54
[2024-02-06 19:18:23]
User719512 - Posts: 264
Posting here for others who might stumble upon this thread.
ChatGPT is very good at answering questions like this as well. StackOverflow as well.


std::list<int> ParseCommaSeparatedIntegers(const std::string& input)
{
std::list<int> result;
std::istringstream iss(input);
std::string token;

while (std::getline(iss, token, ',')) {
if (!token.empty()) {
result.push_back(std::stoi(token));
}
}

return result;
}
...
std::list<int> items = ParseCommaSeparatedIntegers(input);
for (int item: items)
{
...

[2024-02-06 19:38:32]
Ticks - Posts: 183
That's more complex code than what I used.
I only used 8 lines of code and no looping was required.

I also tried ChatGPT before posting on here.
ChatGPT is not Merlin and does not provide all answers.
In fact, ChatGPT gave up and told me it couldn't provide any additional answers.
Date Time Of Last Edit: 2024-02-06 19:39:28
[2024-02-06 20:11:00]
User719512 - Posts: 264
Post your code Ticks, maybe others will want your 8 lines over my example. I only posted something since you said you could not find an answer when searching this forum.

Mine is designed to be a function, reusable, and componentized and also handle test cases like:
""
"1"
"1,-1,2"
"1, 2, -3,4,5"
and will ignore alpha characters for a bad input like "1,2a,3"

No right or wrong solution depending on your needs and error checking requirements.
[2024-02-06 20:35:57]
Ticks - Posts: 183
My code is inline and targeted to just grab the values I need from the file.
I'm not a SC coding expert. So my code is probably considered 5-year-old level coding by expert standards.

So, if anyone finds this thread. I will recommend that they use the advance code provided by User719512.
[2024-02-06 20:57:22]
User719512 - Posts: 264
All good Ticks. We all start somewhere, and all code goes thru iterations and refactoring. If the code works, then it works!
Happy you were able to solve your problem and are making progress.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account