Parsing Varialve # of Values

Discussion in 'C++' started by Mike Copeland, Dec 20, 2011.

  1. 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
    Mike Copeland, Dec 20, 2011
    #1
    1. Advertising

  2. 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.
    Juha Nieminen, Dec 20, 2011
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Dec 20, 2011
    #3
  4. 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.
    Juha Nieminen, Dec 20, 2011
    #4
  5. 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<}}
    Mike Copeland, Dec 20, 2011
    #5
  6. 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).
    Juha Nieminen, Dec 21, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    863
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    567
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    597
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    422
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    715
    John Levine
    Feb 2, 2012
Loading...

Share This Page