Announcement

Collapse
No announcement yet.

Detecting $CRLF's in a string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detecting $CRLF's in a string

    How do you count the number of $CRLF's in a string?

    Eg:
    For i = 1 to 10
    MyString = MyString + "Sometext" + $CRLF
    Next

    How do I go through MyString and find each $CRLF now?

    ------------------
    Kind Regards
    Mike

  • #2
    If to only count, use: lRes = TALLY(MyString, $CRLF).
    If to look for positions, use:
    Code:
      DO
         pos = INSTR(pos + 1, MyString, $CRLF)
         'MSGBOX str$(pos)
      LOOP WHILE pos

    ------------------

    Comment


    • #3
      Great! I didnt think you could treat them like regular Chars

      Thx Borje

      ------------------
      Kind Regards
      Mike

      Comment


      • #4
        There are at least 2 ways.
        Using INSTR or a for loop.
        check for $CRLF or chr$(13)+chr$(10)
        possibly using parse$ also.
        Code:
           strng$="test"+$crlf
           strng$=strng$+"xxx"+$CRLF+"zxcv"+$CRLF
           l&=88
           DO
             l&=INSTR(l&+1,strng$,$CRLF)
             if l& then count&=count&+1 
             IF l&=0 OR l&=LEN(strng$)-1 THEN EXIT DO
           LOOP
        
          count&=0
          FOR k&=1 TO LEN(strng$)
            IF MID$(strng$,k&,2)=$CRLF THEN count&=count&+1:k&=k&+1
          NEXT
        ------------------




        [This message has been edited by Fred Buffington (edited May 03, 2001).]
        Client Writeup for the CPA

        buffs.proboards2.com

        Links Page

        Comment


        • #5
          The easiest way:

          lCount = PARSECOUNT(STRING, $CRLF )-1

          Cheers

          Florent

          ------------------

          Comment


          • #6
            TALLY is 'slightly' less work than PARSECOUNT since you don't need to subtract one.

            Using INSTR in a loop is even less efficient, since you can do it with TALLY or PARSECOUNT with one line of code.

            Borje's code is probably the most efficient way to enumerate the actual positions. If you just want to extract the text between the %CRLF pairs, then PARSE$ is the perfect solution.

            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              Efficient?
              I think Parse$ will start at byte 1 at each request?
              What if i have 1mb to search in..?



              ------------------
              hellobasic

              Comment


              • #8
                heh! To be clear, PARSE$ functions perfectly with any size string, from zero bytes through to two gigibytes.

                The point is that PARSE$ provides a way to extract the text without writing masses of code. It is "perfect" in the sense that you can be more efficient in writing your own code.

                What this thread clearly demonstrates is that there are any number of ways to skin a cat... PowerBASIC provide the tools to write application code, and it provides functionality that you can exploit if you want super performance (pointers, inline assembly, etc).

                How you use these tools and my advice is completely up to you. To use one of Bob's expressions: You can attach any value you like to my comments.

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Ouch, please don't - I tend to attach wrong values all the time..

                  But seriously - I agree, almost unlimited flexibility is probably *the*
                  best feature in all PowerBasic compilers. A far more efficient way to
                  find positions is to use pointers. I love pointers - can't live without
                  them, because they provide such a clean and fast way to manipulate text.

                  Following little sample does the same job as DO/LOOP above but is much
                  faster, even a MB string is processed in a fraction of a second:
                  Code:
                    LOCAL I AS LONG, Letter AS BYTE PTR
                    Letter = STRPTR(MyString)  'point Letter to start address of string
                    FOR I = 1 TO LEN(MyString) 'loop through entire text
                       IF @Letter = 13 THEN    'Letter's value is 13 = Carriage Return = $CR
                                               'I is position in string - do what you like with it..
                       END IF
                       INCR Letter             'set pointer to to next character
                    NEXT
                  However, if goal is to parse out a certain line in a text, PARSE$
                  definitely is the one to use.


                  ------------------

                  Comment


                  • #10
                    What this thread clearly demonstrates is that there are any number of ways to skin a cat...How you use these tools and my advice is completely up to you..
                    You mean, "It's not the paintbrush, it's the artist?"

                    MCM
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      Lance,


                      You said that "any size string, from zero bytes through to two gigibytes"? this tells me that PB uses LONGS internally for string length. Why on earth would they use LONG instead of DWORD. I don't think you can have strings of negitive length Does this have anything to do with how windows deals with OEM strings?

                      ------------------
                      Cheers!

                      Comment


                      • #12
                        Thanks for your answers. I find these different methods very revealing. A few times befor I have seen a new way to approach my writing from the simplist question

                        This is how i used it:
                        IF Confirm AND AttachStr+ResourceStr+ConsoleStr+CompressStr+CRCStr <> "" THEN
                        ResourceStr = LEFT$(ResourceStr, LEN(ResourceStr)-2) ' trim off last $CRLFs
                        IF ResourceStr <> "" THEN DisplayStr = DisplayStr + $CRLF + $CRLF + ResourceStr
                        IF AttachStr <> "" THEN DisplayStr = DisplayStr + $CRLF + $CRLF + AttachStr
                        IF ConsoleStr <> "" THEN DisplayStr = DisplayStr + $CRLF + $CRLF + ConsoleStr
                        IF CompressStr <> "" THEN DisplayStr = DisplayStr + $CRLF + $CRLF + CompressStr
                        IF CRCStr <> "" THEN DisplayStr = DisplayStr + $CRLF + $CRLF + CRCStr
                        DisplayStr = RIGHT$(DisplayStr, LEN(DisplayStr)-4) ' trim off leading $CRLFs and space
                        Ht = (TALLY(DisplayStr, $CRLF)+1)*9 + 24 ' 24 for OK button
                        DIALOG NEW 0 ,"Compile Information", , , 110, Ht, %WS_POPUP OR %WS_DLGFRAME TO hDlg
                        CONTROL ADD LABEL, hDlg, 110, DisplayStr, 4, 4, 105, Ht-5,
                        CONTROL ADD BUTTON, hDlg, 102, "OK", 35, Ht-18, 40, 16, 1 CALL OKProc
                        DIALOG SHOW MODAL hDlg ' Show confirmation window
                        END IF

                        This makes a window just the right height for any combination of the componant strings.

                        ------------------
                        Kind Regards
                        Mike

                        Comment


                        • #13
                          Originally posted by mark smit:
                          You said that "any size string, from zero bytes through to two gigibytes"? this tells me that PB uses LONGS internally for string length. Why on earth would they use LONG instead of DWORD. I don't think you can have strings of negitive length Does this have anything to do with how windows deals with OEM strings?
                          2Gb for a single string is a 32-bit Windows limit - most Win32 versions only provide a maximum of 2Gb of application address space.

                          In this sense, comparing LONG and DWORD is irrelevant.


                          ------------------
                          Lance
                          PowerBASIC Support
                          mailto:[email protected][email protected]</A>
                          Lance
                          mailto:[email protected]

                          Comment


                          • #14
                            Why on earth would they use LONG instead of DWORD. I don't think you can have strings of negitive length
                            How do you think we used FORTRAN to patch programs & hack the OS on the mainframe in the old days?



                            ------------------
                            [email protected]
                            http://www.northnet.org/bdurland
                            Real programmers use a magnetized needle and a steady hand

                            Comment

                            Working...
                            X