Announcement

Collapse
No announcement yet.

parsecount

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

  • Jeff Blakeney
    replied
    Mel, I'm with Michael on this. In your specific circumstance, TALLY and PARSECOUNT wouldn't work for you without some help so you had to come up with a different way to handle it. I think your idea of changing single spaces to double spaces would have been easy to accomplish with code like this:

    Code:
    m$ = " and and and God the and and "
    s$ = " and "
    REPLACE $SPC WITH $SPC + $SPC IN m$    ' make single spaces into double spaces
    m$ = $SPC + TRIM$(m$) + $SPC           ' make sure the string starts and ends with a space
    t = TALLY m$, s$
    In this case, t should come out as 5 which is what you were after.

    Shawn, in your case, this code will return the value 2 for you:

    Code:
    myString$="userPW=shawn&userID=shawn&ver=97&samID_1=555&dvm_555=75&tsd_555=3&krcCasa_555_1=&hemaID_555_1=&s1a_555_1=&s1b_555_1=&s1c_555_1=&s2a_555_1=&s2b_555_1=&s2c_555_1=&divisor_555_1=.77&hemaID_555_2=&s1a_555_2=&s1b_555_2=&s1c_555_2=&s2a_555_2=&s2b_555_2=&s2c_555_2=&divisor_555_2=.77&ctech_555=shawn&samID_2=777&dvm_777=47&tsd_777=0&krcCasa_777_1=&hemaID_777_1=&s1a_777_1=&s1b_777_1=&s1c_777_1=&s2a_777_1=&s2b_777_1=&s2c_777_1=&divisor_777_1=.77&ctech_777=shawn"
    t = TALLY myString$, "samID_"

    Leave a comment:


  • Michael Mattias
    replied
    >starting position of INSTR by 1 or (LEN(MatchString) - 1).

    To start after the token, you should increment by LEN(MatchString), not LEN(Matchstring)-1.

    There are five occurences of " and " only if you allow for the trailing space of one delimiter to also be the leading space of an immediately adjacent delimiter. As I said, maybe this is what you want in this application, in which case TALLY and PARSECOUNT are the wrong tools, since they do not support 'overlap.'

    However, just because TALLY and PARSECOUNT don't work for your application, you cannot say they are working incorrectly.

    Leave a comment:


  • Mel Bishop
    replied
    I know you have to have the last word on pretty much everything, MM, so this will probably be my final post in this thread.

    In the example program, there five occurances. Count them manually, with a clear head, if you can.

    ...Your INSTR code is incorrect. You are starting your next loop following the first character...
    In the example I provided Sean, you have the option of incrementing the starting position of INSTR by 1 or (LEN(MatchString) - 1). Either one will produce the correct result of 5.

    Leave a comment:


  • Michael Mattias
    replied
    Yes, there are three (3) occurences of " and " in string, assuming any one character may only be used once, so TALLY is correct.

    Yes, there are four (4) strings delimited by the three delimiters, so PARSECOUNT is correct, too.

    Your INSTR code is incorrect. You are starting your next loop following the first character of the " and " (space, 'and', space) delimiter, instead of after the last character of that delimiter.

    As I said, the intrinsic functions do not 'overlap'... any one character of a multi-character delimiter can only 'belong' to one occurrence of that multi-character delimiter.

    Maybe not what you want for this application, but these functions work as adver..., er work the way they have always worked. The doc is very weak on this point and does not qualify as advertising.

    MCM
    Last edited by Michael Mattias; 19 May 2009, 09:36 AM.

    Leave a comment:


  • Mel Bishop
    replied
    You say the number 3 is correct? So why is ParseCount finding 4?

    And you are also saying the other two MatchString's doesn't count? It sure does to me, buddy.

    There are, in fact, five occurances of " and " in MainString. All five have a leading/trailing space and they have to be counted.

    Leave a comment:


  • Michael Mattias
    replied
    I ran your code in PB/CC 5.01 and TALLY is correctly reporting three.

    There is no "overlap" if that's what you are looking for.

    You are 'using up' those spaces with occurences of the delimiter.

    Code:
    m$ = " and and and God the and and "
    s$ = " and "
    ==> 
    "[COLOR="Red"]<sp>and<sp>[/COLOR]and[COLOR="Red"]<sp>and<sp>[/COLOR]God<sp>the[COLOR="Red"]<sp>and<sp>[/COLOR]and<sp>"
    I count three occurs of "<sp>and<sp>." So did TALLY.


    MCM
    Last edited by Michael Mattias; 19 May 2009, 08:48 AM.

    Leave a comment:


  • Mel Bishop
    replied
    Originally posted by Michael Mattias View Post
    ...And TALLY won't?...
    Under certain circumstances, no. Click on my link in my first reply and read my 3rd post.
    Code:
    FUNCTION PBMAIN () AS LONG
        COLOR 14,1
        CLS
        LOCAL t, q, p AS LONG
        m$ = " and and and God the and and "
        s$ = " and "
        t = TALLY(m$,s$)      : PRINT;"     Tally: ";t
    
        t = PARSECOUNT(m$,s$) : PRINT;"ParseCount: ";t
    
        q = 1
        t = 0
        DO
        p = INSTR(q,m$, s$)
        IF p = 0 THEN EXIT LOOP
        INCR t
        q = p + 1
        LOOP
        PRINT;"   Do/Loop: ";t
    
        WAITKEY$
        END FUNCTION
    Last edited by Mel Bishop; 19 May 2009, 08:36 AM. Reason: Providing an example

    Leave a comment:


  • Michael Mattias
    replied
    >This [INSTR] will give you an accurate count of MatchString$

    And TALLY won't? Code not shown.

    Leave a comment:


  • Guy Dombrowski
    replied
    Also you could add UCASE$ to your test

    Leave a comment:


  • Mel Bishop
    replied
    You'd be better off using INSTR. I'm typing this off the cuff so you may have to tweek it.

    Code:
    q = 1
    do
    p = instr(q,MainString, MatchString)
    if p = 0 then exit loop
    incr counter
    q = p + 1                  'or q = q + (len(MatchString) - 1)
    loop
    This will give you an accurate count of MatchString$
    Last edited by Mel Bishop; 18 May 2009, 04:04 PM.

    Leave a comment:


  • Shawn Anderson
    replied
    TALLY !

    thanks

    Leave a comment:


  • Shawn Anderson
    replied
    OK, I think I understand why.

    ParseCount doesn't tell me how many of "samID_" there are (by design).
    It is using "samID_" as the delimiter. Therefore there are 3 segments, divided up by 2 "samID_" delimiters.

    I think if I just subtract 1 from my ParseCount, I'll be OK.
    Is there a built-in PB command that just counts how many of x$ appears in y$?

    added later: we posted over each other... you guys are fast... thanks!

    Leave a comment:


  • Mel Bishop
    replied


    And youll get pretty much the same results using TALLY.
    Last edited by Mel Bishop; 18 May 2009, 03:43 PM.

    Leave a comment:


  • Bob Zale
    replied
    There are two delimiters separating 3 data items.

    Bob Zale

    Leave a comment:


  • Shawn Anderson
    started a topic parsecount

    parsecount

    Code:
    myString$="userPW=shawn&userID=shawn&ver=97&samID_1=555&dvm_555=75&tsd_555=3&krcCasa_555_1=&hemaID_555_1=&s1a_555_1=&s1b_555_1=&s1c_555_1=&s2a_555_1=&s2b_555_1=&s2c_555_1=&divisor_555_1=.77&hemaID_555_2=&s1a_555_2=&s1b_555_2=&s1c_555_2=&s2a_555_2=&s2b_555_2=&s2c_555_2=&divisor_555_2=.77&ctech_555=shawn&samID_2=777&dvm_777=47&tsd_777=0&krcCasa_777_1=&hemaID_777_1=&s1a_777_1=&s1b_777_1=&s1c_777_1=&s2a_777_1=&s2b_777_1=&s2c_777_1=&divisor_777_1=.77&ctech_777=shawn"
    parseCount(myString$,"samID_") returns 3.

    Why? I'm expecting 2.
Working...
X