Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Parsing (a Series of) Variables

Reply
Thread Tools

Parsing (a Series of) Variables

 
 
Mike Copeland
Guest
Posts: n/a
 
      03-24-2013
How do I (or can I?) parse a variable number of numeric values from a
line of text? Below is an example of a data line I need to parse (14
values, but there can be more or less):

1 37 36 4 7 5 6 8 9 10 20 32 23 16

I'm currently using the routine below, which works in a sense, but I
have to ignore blank values in the output vector. In the example data,
I get 20 vector elements, but I have to ignore 6 of them. 8<{{

typedef vector<string> TOKENS1; // parsing structure
TOKENS1 tokArray;
void parseSpace(string line) // parse & store blank-separated tokens
{
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 tokArray.push_back(tok1);
} // while
return;
} // parseSpace

I assume there is a better way to parse such data (it might involve
stringstream). If so, how would I do such a thing in C++? TIA
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-24-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) (Mike Copeland) writes:
> How do I (or can I?) parse a variable number of numeric values from a
>line of text? Below is an example of a data line I need to parse (14
>values, but there can be more or less):
> 1 37 36 4 7 5 6 8 9 10 20 32 23 16


#include <iostream>
#include <ostream>
#include <sstream>

int main()
{ ::std::stringstream source{ "1 37 36 4 7 5 6 8 9 10 20 32 23 16" };
for( int i; source >> i; )::std::cout << i << '\n'; }

> I'm currently using the routine below, which works in a sense, but I
>have to ignore blank values in the output vector. In the example data,
>I get 20 vector elements, but I have to ignore 6 of them. 8<{{


What does »ignore blank values in the output vector« mean?
(Which output vector? What is a »blank value«? What is the
operation »ignore« supposed to do?)

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-24-2013
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>(Which output vector? What is a »blank value«? What is the
>operation »ignore« supposed to do?)


Sorry, in the meantime, I saw that there is an output vector
in the example code. So that answers my first question.

 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      03-24-2013
In article <(E-Mail Removed)-berlin.de>,
(E-Mail Removed)-berlin.de says...
> > How do I (or can I?) parse a variable number of numeric values from a
> >line of text? Below is an example of a data line I need to parse (14
> >values, but there can be more or less):
> > 1 37 36 4 7 5 6 8 9 10 20 32 23 16

>
> #include <iostream>
> #include <ostream>
> #include <sstream>
>
> int main()
> { ::std::stringstream source{ "1 37 36 4 7 5 6 8 9 10 20 32 23 16" };
> for( int i; source >> i; )::std::cout << i << '\n'; }
>
> > I'm currently using the routine below, which works in a sense, but I
> >have to ignore blank values in the output vector. In the example data,
> >I get 20 vector elements, but I have to ignore 6 of them. 8<{{

>
> What does »ignore blank values in the output vector« mean?
> (Which output vector? What is a »blank value«? What is the
> operation »ignore« supposed to do?)


Ultimately, I want to store the converted values into an array
(perhaps another vector). In your example, I don't understand why the
"for loop" passes over the data values. Also, I'm not interested in
displaying the numbers, but instead want to store them somewhere. The
"cout" doesn't do that for me, and I don't see how to access/store each
value as it passes through the loop. 8<{{
Please advise. TIA
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-24-2013
(E-Mail Removed) (Mike Copeland) writes:
>"cout" doesn't do that for me, and I don't see how to access/store each
>value as it passes through the loop. 8<{{


#include <iostream>
#include <ostream>
#include <sstream>
#include <vector>

int main()
{ using number = int;
::std::vector<number> vector;
::std::stringstream source{ "1 37 36 4 7 5 6 8 9 10 20 32 23 16" };
for( number i; source >> i; )vector.push_back( i ); }

 
Reply With Quote
 
Luca Risolia
Guest
Posts: n/a
 
      03-24-2013
On 24/03/2013 01:38, Mike Copeland wrote:
> How do I (or can I?) parse a variable number of numeric values from a
> line of text? Below is an example of a data line I need to parse (14
> values, but there can be more or less):
>
> 1 37 36 4 7 5 6 8 9 10 20 32 23 16


Isn't this the third time that you ask the same question?

https://groups.google.com/forum/?fro...++/lL6tGQsYtGc

https://groups.google.com/forum/?fro...Q/JsN7DZHOPx8J


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-24-2013
(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>#include <iostream>
>#include <ostream>
>#include <sstream>
>#include <vector>


>int main()
>{ using number = int;
> ::std::vector<number> vector;
> ::std::stringstream source{ "1 37 36 4 7 5 6 8 9 10 20 32 23 16" };
> for( number i; source >> i; )vector.push_back( i ); }


Refactor »rename variable«: »vector« -> »target«
(corresponding to »source«), »i« -> »num« (corresponding to
»number«) and removed two include directives (untested):

#include <sstream>
#include <vector>

int main()
{ using number = int;
::std::stringstream source{ "1 37 36 4 7 5 6 8 9 10 20 32 23 16" };
::std::vector<number> target;
for( number num; source >> num; )target.push_back( num ); }

 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      03-24-2013
In article <kilq21$fg1$(E-Mail Removed)>,
(E-Mail Removed) says...
> > How do I (or can I?) parse a variable number of numeric values from a
> > line of text? Below is an example of a data line I need to parse (14
> > values, but there can be more or less):
> >
> > 1 37 36 4 7 5 6 8 9 10 20 32 23 16

>
> Isn't this the third time that you ask the same question?
>
> https://groups.google.com/forum/?fro...++/lL6tGQsYtGc
>
> https://groups.google.com/forum/?fro...Q/JsN7DZHOPx8J
>

It is. However, as I've tried to work with solutions I've received
I've learned that the approaches I've taken aren't as good as I'd like.
Consequently, I've had to refine my query...and this has been the
result.
Sorry if you consider it a waste of time. I continue to work at
improving my skills, as I evolve from procedural coding to more up-to-
date technologies and methods. At my age (73), it's not as easy as it
may be for most here.
My apologies...
 
Reply With Quote
 
Haochen Xie
Guest
Posts: n/a
 
      03-24-2013
Mike Copeland wrote:
> Ultimately, I want to store the converted values into an array
> (perhaps another vector). In your example, I don't understand why the
> "for loop" passes over the data values.


OK, so since Stefan has already given you the version with a vector, I'm
just going to explain why the for loop would work.

The for loop is like this:

for(int i; source >> i; )
collection.push_back(i); // The collection is the vector

it works because "source >> i" would return a reference to the stream,
in this case, source. so as of its return value, it is the same to put
source in the test field. And for a istream, try to evaluate it as a
bool (actually a inexplicit type convention happens here from istream&
to bool), it will return true if it's in a "good" state, and false
otherwise. So if there's no more stuff in the buffer, the source would
be stated of eof (source.eof() will return true), which is not a "good"
state so the expression will return false to end the loop.

And my version to your function is like (to make the vector a return
value and rename the function):

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
using namespace std;

vector<int> parseLine(string line)
{
vector<int> coll; // "coll" for "collection", it's just my naming.
istringstream source(line);
for(int x; source >> x; )
coll.push_back(x);

return coll;
}

For a full version of the program with a test, see
<http://codepad.org/E5p2UR2r>.

--
---------------------
Posted by Haochen Xie
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      03-24-2013
Mike Copeland wrote:

> How do I (or can I?) parse a variable number of numeric values from a
> line of text? Below is an example of a data line I need to parse (14
> values, but there can be more or less):


Do you need to parse only positive integer values, or are you looking to
parse also decimal and exponential numbers?


Rui Maciel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Anyone clustering ASA5000-series boxes with 3000-series VPN concentrators Heath Roberts Cisco 0 10-27-2006 02:45 PM
Difference between Kodak CX series and LS series zxcvar Digital Photography 3 11-05-2004 02:10 PM
2600 series and 1700 series godwill Cisco 4 04-09-2004 08:38 PM
What is the difference between A Series, G Series and S series of Canon Cameras zxcvar Digital Photography 3 09-09-2003 01:30 AM



Advertisments