Okay, I just want to be able to easily parse a string, but I have -no- idea how... I know someone else will though!
Okay, I just want to be able to easily parse a string, but I have -no- idea how... I know someone else will though!
write a simple finite state machine
-beats his head on his keyboard-
Huh?
a vague question deserves a vague answer
Use stringstream or find() and substr().
Use a regular expression.
rofl rocky
sounds slow, a FSM would be much faster, do things in one pass is always better than multiple find callsOriginally Posted by Daved
>> a FSM would be much faster, do things in one pass is always better than multiple find calls
I disagree. It depends on the situation, and as you noted this situation is quite vague. There's no reason to spend time or effort coding an FSM when a simple solution will work just as well. Premature optimization and all that.
fsms are simple to write, one for loop, check a few states. usually when someone says they want to "parse text", they have something like "field1;field2;field3;field4;...;field20;". In this case it would be simple to write an fsm.
If the input is like rockytriton describes, you could just use a stringstream object to parse the text in one pass.
Output:Code:#include <iostream> #include <string> #include <sstream> using namespace std; int main(int argc, char *argv[]) { string text = "field1;field2;field3;field4;field20;" ; stringstream ss(text); while( ss ) { string field ; getline( ss, field, ';' ); cout << field << "\n" ; } cin.get(); return 0 ; }
Code:field1 field2 field3 field4 field20
there you go, even better
>> In this case it would be simple to write an fsm.
Maybe I'm not understanding what you mean. Could you convert Dante Shamest's program to use a finite state machine?
>> even better
That uses a stringstream, so I don't understand your problem with my original suggestion.
ok, that was a bad example, now that I started writing an fsm for it, I relaized it doesn't need more than 1 state. it would make more sense for something that would parse data out of html tags, that's the last thing I did that I used an fsm to implement. In this case, it's still pretty simple and would be much faster than running through multiple finds for '<' and ">' or '/>'.
>> That uses a stringstream, so I don't understand your problem with my original suggestion.
no problem really, I didn't know about the changing the delimiter feature of getline(), it was really the find() and substr() part, it just makes me think of looping through with multiple finds.
Ah yes. If the input was HTML, a FSM would work very well.it would make more sense for something that would parse data out of html tags, that's the last thing I did that I used an fsm to implement.