Announcement

Collapse
No announcement yet.

Tally Wrong Choice

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

  • #21
    Looking for an exact word ice
    Then change match string from "ice" to " ice" to force looking for a word. (Without a space it is just 3 letters surrounded by anything.)

    Cheers,
    Dale

    Comment


    • #22
      What about "ice ice, ice mice ice." I would expect 4.
      Pierre,
      Nice, passes Rods's post #11.

      Dale,
      change "ice" to "ice"
      ?
      How long is an idea? Write it down.

      Comment


      • #23
        Originally posted by Mike Doty View Post
        Code:
        Looking for an exact word ice
        ice. ice, "ice" and others are also valid.
        FUNCTION PBMAIN () AS LONG
        ? STR$(TALLY("dice, spice lice icey","ice")) 'tally finds 4, but 0 are the word ice
        END FUNCTION'
        Where is "I would expect 4" in post 19. Sorry if I simply can't see it.

        Dale

        Comment


        • #24
          Mike,
          Any man that have passed Rod's test is an happy man ! :-)

          Dale,
          Post 11 and 22

          Comment


          • #25
            Pierre,
            Added LenSubString
            Code:
            #DIM ALL
            FUNCTION PBMAIN () AS LONG
             LOCAL sMainString AS STRING
             LOCAL sSubString AS STRING
             LOCAL index AS LONG
             LOCAL WordCount AS LONG
             LOCAL LenSubString AS LONG
            
             sMainString = "ice ice, ice mice iceberg ice$ ice."
             sSubString = "ice"
             LenSubString = LEN(sSubString)
             sMainString = SHRINK$(sMainString, $WHITESPACE & ",.?!=%$&") '<- Add all non word characters
             FOR index = 1 TO PARSECOUNT(sMainString, sSubString)
              IF LEN(PARSE$(sMainString, $SPC, index)) = LenSubString THEN INCR WordCount
             NEXT
             ? STR$(WordCount)
            END FUNCTION

            Now using SHRINK$ from Pierre this will also work.
            Code:
            FUNCTION PBMAIN() AS LONG
             LOCAL sMain, sSearch AS STRING
             sMain  = "ice ice, ice mice ice."
             sSearch= "ice"
             ? STR$(WordCount(sMain,sSearch))
            END FUNCTION
            
            FUNCTION WordCount(BYVAL sMain AS STRING, BYVAL SearchFor AS STRING) AS LONG
            
             LOCAL i,icount,LengthMinus1 AS LONG
            
             sMain = SHRINK$(sMain, $WHITESPACE & ",.?!=%$&") '<- Add all non word characters
             sMain = WRAP$(sMain," "," ")
            
             SearchFor = WRAP$(SearchFor,$SPC,$SPC)
            
             LengthMinus1 = LEN(SearchFor) -1
             i = INSTR(sMain,SearchFor)
             WHILE i
              INCR iCount
              i = INSTR(i+LengthMinus1,sMain,SearchFor)
             WEND
             FUNCTION = iCount
             'https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/795149-tally-wrong-choice#post795163
            END FUNCTION
            How long is an idea? Write it down.

            Comment


            • #26
              Another method:

              (If you want to search for "words" containing apostrophes and/or hyphens, modify strNonLetters)

              '
              Code:
              #COMPILE EXE
              #DIM ALL
              #INCLUDE ONCE "WIN32API.INC"
              
              
              FUNCTION PBMAIN () AS LONG
                  LOCAL strtarget,strSearch AS STRING
                  strTarget = "ice ice iceberg ice$" & $CRLF & "rice" & $TAB & "ice,Ice."
                  strSearch = "ice"
                  ? STR$(TallyWholeWords(strTarget,strSearch,0)) & " matches"
              END FUNCTION
              
              FUNCTION TallyWholeWords(BYVAL strTarget AS STRING, strSearch AS STRING,CaseSensitive AS LONG) AS LONG
                  LOCAL strNonLetters,strReplacer AS STRING
                  strNonletters = CHR$(1 TO 64,91 TO 96,123 TO 255)
                  strReplacer = STRING$(LEN(strNonLetters),CHR$(26)) ' ASCII 26 = SUBstitute
              
                  REPLACE ANY strNonLetters WITH strReplacer IN strTarget
                  strTarget =SHRINK$(strTarget,CHR$(26))
                  REPLACE CHR$(26) WITH CHR$(26,26) IN strTarget
                  strTarget = CHR$(26) & strTarget & CHR$(26)
              
                  IF CaseSensitive THEN
                      FUNCTION = TALLY(strTarget, CHR$(26) & strSearch & CHR$(26))
                  ELSE
                      FUNCTION = TALLY(UCASE$(strTarget), CHR$(26) & UCASE$(strSearch) & CHR$(26))
                  END IF
              
              END FUNCTION
              '

              Comment


              • #27
                Stuart TALLY (no looping) and CaseSensitive flag.
                Code:
                FUNCTION PBMAIN AS LONG
                 LOCAL sMain,sMatch AS STRING,CaseSensitive AS LONG
                 sMain  = "ice ice, ice mice ice."
                 sMatch = "ice"
                 ? "Pierre"+STR$(Pierre(sMain,sMatch))+$CR +"Stuart"+STR$(Stuart(sMain,sMatch,CaseSensitive))+$CR+"Mike"+STR$(Mike(sMain,sMatch))
                END FUNCTION
                '
                FUNCTION Pierre(BYVAL sMain AS STRING,BYVAL sMatch AS STRING) AS LONG
                 LOCAL index        AS LONG
                 LOCAL WordCount    AS LONG
                 LOCAL LenSubString AS LONG
                 LenSubString = LEN(sMatch)
                 sMain = SHRINK$(sMain, $WHITESPACE & ",.?!=%$&") '<- Add all non word characters
                 FOR index = 1 TO PARSECOUNT(sMain, sMatch)
                  IF LEN(PARSE$(sMain, $SPC, index)) = LenSubString THEN INCR WordCount
                 NEXT
                 FUNCTION = WordCount
                END FUNCTION
                '
                FUNCTION Stuart(BYVAL strTarget AS STRING, strSearch AS STRING,CaseSensitive AS LONG) AS LONG
                 LOCAL strNonLetters,strReplacer AS STRING
                 strNonletters = CHR$(1 TO 64,91 TO 96,123 TO 255)
                 strReplacer = STRING$(LEN(strNonLetters),CHR$(26)) ' ASCII 26 = SUBstitute
                 REPLACE ANY strNonLetters WITH strReplacer IN strTarget
                 strTarget =SHRINK$(strTarget,CHR$(26))
                 REPLACE CHR$(26) WITH CHR$(26,26) IN strTarget
                 strTarget = CHR$(26) & strTarget & CHR$(26)
                 IF CaseSensitive THEN
                  FUNCTION = TALLY(strTarget, CHR$(26) & strSearch & CHR$(26))
                 ELSE
                  FUNCTION = TALLY(UCASE$(strTarget), CHR$(26) & UCASE$(strSearch) & CHR$(26))
                 END IF
                END FUNCTION
                '
                FUNCTION Mike(BYVAL sMain AS STRING, BYVAL sMatch AS STRING) AS LONG
                 LOCAL i,icount,LengthMinus1 AS LONG
                 sMain = SHRINK$(sMain, $WHITESPACE & ",.?!=%$&") '<- Add all non word characters
                 sMain = WRAP$(sMain," "," "): sMatch = WRAP$(sMatch,$SPC,$SPC)
                 LengthMinus1 = LEN(sMatch) -1: i = INSTR(sMain,sMatch)
                 WHILE i:INCR iCount: i = INSTR(i+LengthMinus1,sMain,sMatch):WEND
                 FUNCTION = iCount
                END FUNCTION
                How long is an idea? Write it down.

                Comment


                • #28
                  Originally posted by Mike Doty View Post
                  Test all at same time.
                  My bad! I edited my code without commenting on the edit. Note that it now has a CaseSensitive flag.



                  Comment


                  • #29
                    Tally may be Right Choice
                    Stuart added CaseSensitive flag
                    Code:
                    #DIM ALL
                    FUNCTION PBMAIN ()  AS LONG
                     LOCAL sMain,sMatch AS STRING,CaseSensitive AS LONG
                     sMain  = "ice ice, ice mice ice."
                     sMatch = "ice"
                     ? "Pierre" + STR$(Pierre(sMain,sMatch)) + $CR +_
                       "Stuart" + STR$(Stuart(sMain,sMatch,CaseSensitive)) + $CR +_
                       "Mike"   + STR$(Mike(sMain,sMatch))
                    END FUNCTION
                    '
                    FUNCTION Pierre(BYVAL sMain AS STRING,BYVAL sMatch AS STRING) AS LONG
                     LOCAL index        AS LONG
                     LOCAL WordCount    AS LONG
                     LOCAL LenSubString AS LONG
                     LenSubString = LEN(sMatch)
                     sMain = SHRINK$(sMain, $WHITESPACE & ",.?!=%$&") '<- Add all non word characters
                     FOR index = 1 TO PARSECOUNT(sMain, sMatch)
                      IF LEN(PARSE$(sMain, $SPC, index)) = LenSubString THEN INCR WordCount
                     NEXT
                     FUNCTION = WordCount
                    END FUNCTION
                    '
                    FUNCTION Stuart(BYVAL strTarget AS STRING, strSearch AS STRING,CaseSensitive AS LONG) AS LONG
                        LOCAL strNonLetters,strReplacer AS STRING
                        strNonletters = CHR$(1 TO 64,91 TO 96,123 TO 255)
                        strReplacer = STRING$(LEN(strNonLetters),CHR$(26)) ' ASCII 26 = SUBstitute
                    
                        REPLACE ANY strNonLetters WITH strReplacer IN strTarget
                        strTarget =SHRINK$(strTarget,CHR$(26))
                        REPLACE CHR$(26) WITH CHR$(26,26) IN strTarget
                        strTarget = CHR$(26) & strTarget & CHR$(26)
                    
                        IF CaseSensitive THEN
                            FUNCTION = TALLY(strTarget, CHR$(26) & strSearch & CHR$(26))
                        ELSE
                            FUNCTION = TALLY(UCASE$(strTarget), CHR$(26) & UCASE$(strSearch) & CHR$(26))
                        END IF
                    END FUNCTION
                    '
                    FUNCTION Mike(BYVAL sMain AS STRING, BYVAL sMatch AS STRING) AS LONG
                     LOCAL i,icount,LengthMinus1 AS LONG
                     sMain = SHRINK$(sMain, $WHITESPACE & ",.?!=%$&") '<- Add all non word characters
                     sMain = WRAP$(sMain," "," ")
                     sMatch = WRAP$(sMatch,$SPC,$SPC)
                     LengthMinus1 = LEN(sMatch) -1
                     i = INSTR(sMain,sMatch)
                     WHILE i
                      INCR iCount
                      i = INSTR(i+LengthMinus1,sMain,sMatch)
                     WEND
                     FUNCTION = iCount
                    END FUNCTION
                    How long is an idea? Write it down.

                    Comment


                    • #30
                      The code above would only be used to search for whole words
                      REGEXPR supports searching for whole words. "It's In The Manual"

                      Great demo of REGEXPR including whole word search here... REGEXPR and REGREPL demo January 16, 2002

                      BIAS ALERT: I am not a neutral source on the greatness of that demo.
                      Michael Mattias
                      Tal Systems Inc. (retired)
                      Racine WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #31
                        No need to say...

                        Comment


                        • #32
                          Originally posted by Michael Mattias View Post

                          REGEXPR supports searching for whole words. "It's In The Manual"
                          Great demo of REGEXPR including whole word search here..
                          More apposite demo here:
                          '
                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          
                          FUNCTION PBMAIN () AS LONG
                              LOCAL strtarget,strSearch AS STRING
                              strTarget = "ice ice iceberg ice$" & $CRLF & "rice" & $TAB & "ice,Ice."
                              strSearch = "ice"
                              ? STR$(RegExWholeWords(strTarget,strSearch,0)) & " matches"
                          END FUNCTION
                          
                          FUNCTION RegExWholeWords(BYVAL strTarget AS STRING, strSearch AS STRING,CaseSensitive AS LONG) AS LONG
                              LOCAL lPos,lCount AS LONG
                              LOCAL strMask AS STRING
                              strMask = "\b" & strSearch & "\b"
                              IF CaseSensitive THEN strMask += "\c"
                              REGEXPR strMask IN strTarget TO lPos
                              WHILE lPos > 0
                                  INCR lCount
                                  REGEXPR strMask IN strTarget AT lPos + LEN(strsearch) TO lPos
                              WEND
                              FUNCTION = lCount
                          END FUNCTION
                          '

                          Comment

                          Working...
                          X