M
Mike Copeland
The function below (parseNum) seems convoluted and possibly
faulty...although it seems to work. In the code invocation (far below)
the data is real-world, and I wish to parse only the first 6 numeric
values. The number of values to be parsed varies, but there is always a
"termination value" of some alphabetic value or end-of-line. Thus, I
want this logic to act as though it's a variable-value "scanf".
Please advise if there's a "cleaner" way to do this. TIA
typedef vector<string> TOKENS1; // parsing structures
TOKENS1 tokArray;
size_t parseNum(string line) // Parse numeric value(s)
{
string tok1, tok2;
istringstream iss1(line);
tokArray.clear();
while(getline(iss1, tok1, ' '))
{
if(tok1.find(' ') != string::npos)
{
istringstream iss1(tok1);
while(getline(iss1, tok2, ' '))
{
if(!tok2.empty()) tokArray.push_back(tok2);
} // while
} // if
else
{
if(tok1 == "") continue;
if(isdigit(tok1.at(0))) tokArray.push_back(tok1);
else return tokArray.size();
}
} // while
return tokArray.size();
} // parseNum
char m1[] = " 326 500 11 3900 11 3900 stop 10/29/2011 ";
size_t ii = parseNum(m1);
faulty...although it seems to work. In the code invocation (far below)
the data is real-world, and I wish to parse only the first 6 numeric
values. The number of values to be parsed varies, but there is always a
"termination value" of some alphabetic value or end-of-line. Thus, I
want this logic to act as though it's a variable-value "scanf".
Please advise if there's a "cleaner" way to do this. TIA
typedef vector<string> TOKENS1; // parsing structures
TOKENS1 tokArray;
size_t parseNum(string line) // Parse numeric value(s)
{
string tok1, tok2;
istringstream iss1(line);
tokArray.clear();
while(getline(iss1, tok1, ' '))
{
if(tok1.find(' ') != string::npos)
{
istringstream iss1(tok1);
while(getline(iss1, tok2, ' '))
{
if(!tok2.empty()) tokArray.push_back(tok2);
} // while
} // if
else
{
if(tok1 == "") continue;
if(isdigit(tok1.at(0))) tokArray.push_back(tok1);
else return tokArray.size();
}
} // while
return tokArray.size();
} // parseNum
char m1[] = " 326 500 11 3900 11 3900 stop 10/29/2011 ";
size_t ii = parseNum(m1);