Parsing Varialve # of Values

M

Mike Copeland

I'm looking for a way to parse some variables that are preceded by
the number of variables that follow. For example, I have the following
data:

5 3 4 2 5 1 Phase Count, Order

where the first value defines the number of values that follow (5). I
then want to place the five values in an array. The limits of my data
are 1 through 5; that is, there can be 1-5 values parsed and placed in
the array.
I am currently using a series of sscanfs in a select/case statement
(and I know it's UGLY!)...

switch(phaseCount) // parse phase order values (1-5)
{
case 1: sscanf(s24, "%d", &px[0]); break;
case 2: sscanf(s24, "%d%d", &px[0], &px[1]); break;
case 3: sscanf(s24, "%d%d%d", &px[0], &px[1], &px[2]); break;
case 4: sscanf(s24, "%d%d%d%d", &px[0], &px[1], &px[2], &px[3]);
break;
case 5: sscanf(s24, "%d%d%d%d%d", &px[0], &px[1], &px[2], &px[3],
&px[4]); break;
} // switch

Any thoughts? TIA
 
J

Juha Nieminen

Mike Copeland said:
I'm looking for a way to parse some variables that are preceded by
the number of variables that follow. For example, I have the following
data:

5 3 4 2 5 1 Phase Count, Order

where the first value defines the number of values that follow (5). I
then want to place the five values in an array. The limits of my data
are 1 through 5; that is, there can be 1-5 values parsed and placed in
the array.
I am currently using a series of sscanfs in a select/case statement
(and I know it's UGLY!)...

I don't understand why you don't use a loop to read as many integers
as the first value says.
 
V

Victor Bazarov

I don't understand why you don't use a loop to read as many integers
as the first value says.

.... and it could also be a hidden loop if one uses the first value to
initialize a vector, and then uses std::copy to read the following
values into the vector, from its 'begin()' to its 'end()'... My $0.02.

V
 
J

Juha Nieminen

Victor Bazarov said:
... and it could also be a hidden loop if one uses the first value to
initialize a vector, and then uses std::copy to read the following
values into the vector, from its 'begin()' to its 'end()'... My $0.02.

It actually may be simpler to just write the loop explicitly. The
advantage is that you are not limited to C++ streams.
 
M

Mike Copeland

It actually may be simpler to just write the loop explicitly. The
advantage is that you are not limited to C++ streams.
You guys are beyond me this time. 8<{{
My application here is a small part of a large legacy project I'm
trying to upgrade to use modern C++ & STL constructs - from simple C
code I wrote 20+ years ago. This particular code parses elements of a
parameter data file. At the "head" of the logic, I read text file
strings, determine what type of record is being processed, and use a
switch/case statement to work with the remaining record type data. I
have already read the record am passing a string containing the variable
data in the record to be processed. This is but 1 of 65+ record types
being handled in the code.
Thus, I don't see how I can "(re)read" the elements of the string
data...and the sscanfs were the only way I knew to do it years ago.
Paul's technique works nicely (now that I understand it), but your
and Victor's discussions have me totally confused. 8<}}
 
J

Juha Nieminen

Mike Copeland said:
My application here is a small part of a large legacy project I'm
trying to upgrade to use modern C++ & STL constructs - from simple C
code I wrote 20+ years ago.

Using C++ streams and strings have many advantages over using the
equivalent C functions. For example, it's very easy to read an entire
line of input into a string regardless of how long that line might be.

However, unfortunately there are disadvantages as well. For instance,
in most systems that I know of, C++ streams are significantly slower than
the C equivalents. This is inconsequential if the reading/writing doesn't
need to be fast, but it can become significant when large amounts of data
need to be read/written as fast as possible (typically C++ streams will be
at least twice as slow as C streams).

Memory consumption will also be sometimes higher, for example in cases
where you simply have data behind a char* that you want to parse. If you
try to use eg. a stringstream to do it, that data will typically be
*copied* to the stringstream object, rather than being parsed in-place,
thus increasing memory consumption (and the copying itself taking time).

Also, there are some situations where it's just outright *simpler* to
use a C standard function over the C++ equivalent. For example, if you
want to parse an integer in ascii format from a char*, it's just simpler
to use std::atoi() than a stringstream. (The reverse is, of course, also
true in other cases, such as when reading a variable-length string from
a file, in which case C++ streams make it much simpler.)

So C++ streams are not a panacea over C streams. Sometimes they are
useful and in fact much handier, sometimes they aren't.
Paul's technique works nicely (now that I understand it), but your
and Victor's discussions have me totally confused. 8<}}

Rather than use an STL algorithm, it may in fact be simpler to just
write a for-loop explicitly (rather than your switch block). The
advantage is that you are not limited to using C++ streams, but instead
you can use eg. std::strtol (which, as said, may be more efficient
depending on the situation).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top