Parsing string containing doubles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Lidström

    #1

    Parsing string containing doubles

    Hi,

    I'm currently using this method to extract doubles from a string:

    System::String* sp = S" ";
    System::String* tokens[] = s->Trim()->Split(sp->ToCharArray()) ;
    m_Northing = System::Double: :Parse(tokens[0], nfi);
    m_Easting = System::Double: :Parse(tokens[1], nfi);
    m_Elevation = System::Double: :Parse(tokens[2], nfi);

    However, only strings that contain one " " are correctly parsed. How can I
    do this for strings that contain arbitrary number of " " between doubles?
    I.e.:
    "79812.862 15532.942 1565.570" is ok, while
    "79812.862 15532.942 1565.570" is not ok.

    With C++ I would just use a stringstream. Can I do this with MC++ too? I
    have not yet figure out how.

    Thanks!

    --
    Daniel
  • Niki Estner

    #2
    Re: Parsing string containing doubles

    There is no real equivalent to C++'s scanf or iostreams in .NET. The thing
    that comes closest to these are regular expressions (C# syntax):

    using System.Text.Reg ularExpressions ;
    ....
    Match m = Regex.Match(you rString,
    @"^\s*([0-9.,]+)\s+([0-9.,]+)\s+([0-9.,]+)\s*$");
    if (m.Success)
    {
    Console.WriteLi ne(System.Doubl e.Parse(m.Group s[1].ToString()));
    Console.WriteLi ne(System.Doubl e.Parse(m.Group s[2].ToString()));
    Console.WriteLi ne(System.Doubl e.Parse(m.Group s[3].ToString()));
    }

    However, Regex's are far more powerful, and it may take some time to get
    used to them. It's worth the effort though, IMO.

    Niki

    "Daniel Lidström" <someone@micros oft.com> wrote in
    news:t2mlxmzd75 5v$.1rdd7b0xfxd c2.dlg@40tude.n et...[color=blue]
    > Hi,
    >
    > I'm currently using this method to extract doubles from a string:
    >
    > System::String* sp = S" ";
    > System::String* tokens[] = s->Trim()->Split(sp->ToCharArray()) ;
    > m_Northing = System::Double: :Parse(tokens[0], nfi);
    > m_Easting = System::Double: :Parse(tokens[1], nfi);
    > m_Elevation = System::Double: :Parse(tokens[2], nfi);
    >
    > However, only strings that contain one " " are correctly parsed. How can I
    > do this for strings that contain arbitrary number of " " between doubles?
    > I.e.:
    > "79812.862 15532.942 1565.570" is ok, while
    > "79812.862 15532.942 1565.570" is not ok.
    >
    > With C++ I would just use a stringstream. Can I do this with MC++ too? I
    > have not yet figure out how.
    >
    > Thanks!
    >
    > --
    > Daniel[/color]


    Comment

    • WizyDig

      #3
      Re: Parsing string containing doubles

      Have you considered using replace to clean your string before you call
      split? You could replace the " " with " ". Just a thought then you
      would have to change your other code. It only requires one extra line
      per type of clean up you need to do.

      Wiz

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Niki Estner

        #4
        Re: Parsing string containing doubles

        What if the input is separated by 3 spaces?
        ....or 4?

        Niki

        "WizyDig" <someone@micros oft.com> wrote in
        news:uI%230vnTh EHA.3148@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Have you considered using replace to clean your string before you call
        > split? You could replace the " " with " ". Just a thought then you
        > would have to change your other code. It only requires one extra line
        > per type of clean up you need to do.
        >
        > Wiz
        >
        > *** Sent via Developersdex http://www.developersdex.com ***
        > Don't just participate in USENET...get rewarded for it![/color]


        Comment

        • Daniel Lidström

          #5
          Re: Parsing string containing doubles

          On Wed, 18 Aug 2004 15:44:26 +0200, Niki Estner wrote:
          [color=blue]
          > There is no real equivalent to C++'s scanf or iostreams in .NET. The thing
          > that comes closest to these are regular expressions (C# syntax):
          >
          > using System.Text.Reg ularExpressions ;
          > ...
          > Match m = Regex.Match(you rString,
          > @"^\s*([0-9.,]+)\s+([0-9.,]+)\s+([0-9.,]+)\s*$");
          > if (m.Success)
          > {
          > Console.WriteLi ne(System.Doubl e.Parse(m.Group s[1].ToString()));
          > Console.WriteLi ne(System.Doubl e.Parse(m.Group s[2].ToString()));
          > Console.WriteLi ne(System.Doubl e.Parse(m.Group s[3].ToString()));
          > }
          >
          > However, Regex's are far more powerful, and it may take some time to get
          > used to them. It's worth the effort though, IMO.[/color]

          How would I write this in MC++? I tried this but got compilation errors:

          #using <System.dll>

          using namespace System::Text::R egularExpressio ns;
          ....
          Match* m = Regex::Match(s,
          "^\\s*([0-9.,]+)\\s+([0-9.,]+)\\s+([0-9.,]+)\\s*$");
          if( m->Success ){
          m_Northing = System::Double: :Parse(m->Groups->Item(1)->ToString());
          m_Easting = System::Double: :Parse(m->Groups->Item(2)->ToString());
          m_Elevation = System::Double: :Parse(m->Groups->Item(3)->ToString());
          }

          (113) : error C2064: term does not evaluate to a function taking 1
          arguments
          (113) : error C2227: left of '->ToString' must point to class/struct/union

          113 refers to m_Northing = ... line. Also, is the regular expression
          written to be equivalent with the CS example? What does the @ at the start
          of the CS RE mean?
          Thanks!

          --
          Daniel
          A: Because it messes up the order in which people normally read text.
          Q: Why is top-posting such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Niki Estner

            #6
            Re: Parsing string containing doubles

            "Daniel Lidström" <someone@micros oft.com> wrote in
            news:senj9alb6n g$.18b5cc33b2t9 3.dlg@40tude.ne t...[color=blue]
            > ...
            > How would I write this in MC++? I tried this but got compilation errors:
            > ...[/color]

            Item is an indexer; Try:
            m_Northing = System::Double: :Parse(m->Groups->Item[1]->ToString());
            m_Easting = System::Double: :Parse(m->Groups->Item[2]->ToString());
            m_Elevation = System::Double: :Parse(m->Groups->Item[3]->ToString());
            [color=blue]
            > Also, is the regular expression
            > written to be equivalent with the CS example?[/color]

            Yes, I think so.
            [color=blue]
            > What does the @ at the start of the CS RE mean?[/color]

            In a "normal" C# string you have to escape backslashes like in C. However if
            you prefix the string literal with @, you can put in backslashes like any
            other character. Nice feature for paths or regular expressions.
            Maybe you should have a look at a tool like Expresso
            (http://www12.brinkster.com/ultrapico/Expresso.htm). It's perfect for
            testing a regular expression on sample data, and it can create correct MC++
            code (with correctly escaped backslashes).

            Just curious: why do you use MC++? I tend to use C# where I can, because I
            don't like all those special keywords like "__gc", "__value" and so on.

            Niki


            Comment

            • Daniel Lidström

              #7
              Re: Parsing string containing doubles

              On Thu, 19 Aug 2004 12:21:41 +0200, Niki Estner wrote:
              [color=blue]
              > Maybe you should have a look at a tool like Expresso
              > (http://www12.brinkster.com/ultrapico/Expresso.htm). It's perfect for
              > testing a regular expression on sample data, and it can create correct MC++
              > code (with correctly escaped backslashes).[/color]

              That's a great tool! Many thanks for pointing me to it.

              --
              Daniel

              Comment

              • WizyDig

                #8
                Re: Parsing string containing doubles

                This small chunk of code will do what you are looking for Niki.


                string t = " some string with odd number of spaces";
                int i = t.IndexOf(" ",0);
                int j = t.IndexOf(" ",0);
                while( i > 0 || j > 0)
                {
                t = t.Replace(" "," ");
                i = t.IndexOf(" ",0);
                j = t.IndexOf(" ",0);
                }

                Hope that helps

                -Wiz

                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                Working...