Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

one by one each character

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

  • PBWin one by one each character

    #COMPILER PBWIN 9
    #DIM ALL
    ' An example is worth a thousand words
    '-------------------------------------------------------------------------------------------
    'assuming --> {1234,Q145,Sales,Cost,$120.30} etc
    'In an array that contains numbers and letters {ABC12} need to find only numbers{$1234.50}.
    'It is what it claims this little routine
    ' Best idea welcome
    ' sorry for my bad English
    '-------------------------------------------------------------------------------------------


    'Esta funcion determina si puede usarse como numeral o no en una frase dada
    'al momento que encuentra un elemento que no es numero o punto se sale
    'dando funcion 0 , de lo contrario function -1
    'no admite espacios daria Error no--> CHR$(32)
    '-------------------------------------------------------------------------------------------

    FUNCTION CaptSiEsNumOString(BYVAL Need_Num AS STRING) AS LONG
    ' This routine can be varied to specific needs
    LOCAL char AS STRING
    LOCAL y AS LONG
    ' del 0 al 9 CHR$(48,57)
    FOR y = 1 TO LEN(Need_Num)
    char$ = MID$(Need_Num,y,1) 'one by one each character
    IF char => CHR$(48) AND char=<CHR$(57) OR char=CHR$(46) OR char =CHR$(36) THEN
    FUNCTION = -1
    ELSE
    'It has elements that do not do Number
    FUNCTION= 0
    EXIT FUNCTION
    END IF
    NEXT y
    END FUNCTION

    '------------------------------------------------------------------------------


    FUNCTION PBMAIN () AS LONG
    LOCAL Result AS LONG
    LOCAL Numeral AS STRING
    'Example 1
    numeral = "$459.60" '<-- we need this format
    'Example 2
    ' numeral = "#120" 'or <-- this no
    CaptSiEsNumOString numeral TO result
    IF result <>0 THEN
    REPLACE ".." WITH "." IN Numeral ' detectar 2 puntos dejar uno
    MSGBOX numeral +" is what I need",64,FUNCNAME$
    ELSE
    MSGBOX numeral +" has elements that do not do my Number",64,FUNCNAME$
    END IF
    END FUNCTION

  • #2
    Here is version using PB VERIFY keyword:

    Code:
    #COMPILER PBWIN 9
    #DIM ALL
    
    FUNCTION CaptSiEsNumOString(BYVAL Need_Num AS STRING) AS LONG
      
      IF VERIFY(Need_Num, "0123456798.$") = 0 THEN
        FUNCTION = -1
      ELSE
        FUNCTION = 0
      END IF
    
    END FUNCTION
    
    '------------------------------------------------------------------------------
    
    
    FUNCTION PBMAIN () AS LONG
      LOCAL Result AS LONG
      LOCAL Numeral AS STRING
      
      ' -- Example 1
      Numeral = "$459.60" '<-- we need this format
      
      ' -- Example 2
      'Numeral = "#120" 'or <-- this no
      
      result = CaptSiEsNumOString(Numeral)
    
      IF result <> 0 THEN
        ' [EN] detect 2 or more dots, reduce them to single one
        ' [SP] detectar 2 o mas puntos, dejar uno
        WHILE INSTR(Numeral, "..")
          REPLACE ".." WITH "." IN Numeral
        WEND
    
        MSGBOX numeral +" is what I need", %MB_ICONINFORMATION OR %MB_OK,FUNCNAME$
      ELSE
        MSGBOX numeral +" has elements that do not do my Number", %MB_ICONINFORMATION OR %MB_OK,FUNCNAME$
      END IF
    END FUNCTION

    Thanks for you code,
    Petr

    P.S. Little tip - when posting code, put it between CODE tags, it will preserve indenting.
    [email protected]

    Comment


    • #3
      'Sorry, I will try to explain in English that I did this routine

      Hi Petr Schreiber jr


      'Sorry, I will try to explain in English that I did this routine
      'I had to solve a problem with different price lists that have different positions
      'for the items found there.
      'I wanted to make one that was capable ListView
      'to detect the position of the charge automatically.
      'and this routine is giving me good result . I need is to find the first element of
      'column xx, analyze it and see if it is I need --> example the cost and recalculate

      'This routine is designed for a specific search in the special case where there are several
      'elements which have a different format but we know and is defined as fields Lisview instance
      '{Part Number, Description,' Cost} and used for different lists with different position
      'elements and we need to capture the cost in each list to recalculate .
      'And we know that the cost column is different but in the same Listview.
      'The idea is to automatically detect which column is the cost in different lists Providers

      'I upload to Power Basic detect if someone needs something like this.
      'Oh ... if there is a better idea.
      'Both this forum has given me that I must try to return something

      'I hope you understand my bad English



      'SMALL CHANGE
      '===============================================================================

      FUNCTION CaptSiEsNumOString(BYVAL Need_Num AS STRING) AS LONG
      ' This routine can be varied to specific needs
      LOCAL char AS STRING
      LOCAL y AS LONG
      ' del 0 al 9 CHR$(48,57)
      FOR y = 1 TO LEN(Need_Num)
      char$ = MID$(Need_Num,y,1) 'one by one each character
      ' This routine can be varied to specific needs
      'Small change is detected if the first element of Dollar sign '$' CHR$(36) if not in that position
      'is not cost

      IF y=1 AND char =CHR$(36) _ <---------- detected if the first element of Dollar
      OR char => CHR$(48) AND char=<CHR$(57) OR char=CHR$(46)OR CHAR =CHR$(32) THEN

      'IF char => CHR$(48) AND char=<CHR$(57) OR char=CHR$(46) OR char =CHR$(36) THEN
      FUNCTION = -1
      ELSE
      'It has elements that do not do Number
      FUNCTION= 0
      EXIT FUNCTION
      END IF
      NEXT y
      END FUNCTION

      Eduardo Miranda

      Comment


      • #4
        Hi Eduardo,

        so you do early test whether first character is $, to exclude it from further search, that is smart! Could be done as this too (renamed to IsPrice):
        Code:
        ' -- Transparent syntax of bulletproof version :)
        FUNCTION IsPrice(BYVAL StringForm AS STRING) AS LONG
        
          ' -- Does not start with dollar? Then it is no price
          IF LEFT$(StringForm, 1) <> "$" THEN
            FUNCTION = 0
            EXIT FUNCTION
          END IF
          
          ' -- Remove the $ in temp string
          StringForm = LTRIM$(StringForm, "$")
          
          ' -- Remove zeros from the end - they are not necessary
          StringForm = RTRIM$(StringForm, "0")
          
          ' -- Test just for value
          DIM Value AS CURRENCY
          
          ' -- String to value
          Value = VAL(StringForm)
          
          ' -- Is string to value equal to original string?
          IF FORMAT$(Value) <> StringForm THEN
            FUNCTION = 0
            EXIT FUNCTION
          END IF
          
          ' -- Otherwise it is ok!
          FUNCTION = -1
          
        END FUNCTION

        Petr

        P.S. I attached image of how to use CODE tag
        Attached Files
        [email protected]

        Comment


        • #5
          See also: RETAIN$() function.
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            'char by char

            'char by char
            'Of course we could use char by char to find a space in a string and many other uses

            #COMPILE EXE
            #DIM ALL

            FUNCTION Char_by_Char(BYVAL iString AS STRING) AS STRING
            LOCAL char AS STRING
            LOCAL y ,cuente AS LONG
            LOCAL uno , dos AS STRING
            LOCAL iArray() AS STRING
            ' This routine can be varied to specific needs
            DIM iArray(20)
            FOR y = 1 TO LEN(iString)
            char$ = MID$(iString,y,1) 'one by one each character
            IF char <> CHR$(32) THEN
            iArray(cuente)= iArray(cuente)+char
            ELSE
            INCR cuente
            END IF
            NEXT y
            FOR y = 0 TO UBOUND(iArray(1))
            dos = dos & $CRLF & iArray(y)
            NEXT y
            MSGBOX dos ,64,FUNCNAME$

            END FUNCTION

            '----------------------------------------------------------------------------------------

            FUNCTION PBMAIN () AS LONG
            LOCAL istring AS STRING
            istring="'Of course we could use char by char to find a space in a string and many other uses "
            Char_by_Char(istring)

            END FUNCTION

            Comment


            • #7
              If you like testing for ASCII values, it might become slightly inefficient to use repeated calls to CHR$ and MID$. It is possible to use one very ellegant PB technique instead - overlaying BYTE array over string. It is still very clear to understand, and faaast.

              Code:
              FUNCTION <CustomNameHere>(BYVAL inString AS STRING)
                
                REGISTER i AS LONG    ' -- Fast loops
                LOCAL strLen AS LONG
                strLen = LEN(inString)
                
                ' -- Overlay BYTE (0..255) over STRING results in
                ' -- having the ASCII code immediately
                DIM ASCII(1 TO strLen) AS BYTE AT STRPTR(inString)
                  
                FOR i = 1 TO strLen
                  ' -- Catching spaces for example
                  IF ASCII(i) = 32 THEN ... ' This simple, instead of IF MID$(strLen, i, 1) = CHR$(32)
                  
                  ...
                NEXT
              
                ...
                
              END FUNCTION
              [email protected]

              Comment


              • #8
                char by char

                It is another good option, I think this forum is the option to teach what we know and learn what I do not know
                Thanks for showing me another option

                Character by character That is the question?

                Eduardo Miranda

                Comment

                Working...
                X