Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Parsing Varialve # of Values

Reply
Thread Tools

Parsing Varialve # of Values

 
 
Mike Copeland
Guest
Posts: n/a
 
      12-20-2011
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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      12-20-2011
Mike Copeland <> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-20-2011
On 12/20/2011 3:07 AM, Juha Nieminen wrote:
> Mike Copeland<> wrote:
>> 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.


.... 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-20-2011
Victor Bazarov <> wrote:
> ... 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.
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      12-20-2011
In article <4ef093fe$0$3059$>, d
says...
> Victor Bazarov <> wrote:
> > ... 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.
>

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<}}
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-21-2011
Mike Copeland <> wrote:
> 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).
 
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
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57