Announcement

Collapse
No announcement yet.

Using TRIM$ on Typed Arrays

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

  • Using TRIM$ on Typed Arrays

    Hi

    Does anyone know how to make a typed array variable LEN = 0 if it's
    empty? The problem I'm having is that if the array is atually empty
    the length of the string is 40 so the test below doesn't work. Testing
    on LEN = 40 is not safe either if the variable actually happens to
    hold 40 valid characters. Tested on SPACE$(40) didn't work either.

    TYPE Business_Table
    Company AS STRING * 40
    First AS STRING * 40
    Last AS STRING * 40
    END TYPE

    DIM BusList(1:100) as Business_Table

    first$ = TRIM$(BusList(i&).First) ' : IF first$ = SPACE$(40) THEN first$ = ""
    last$ = TRIM$(BusList(i&).Last) ' : IF last$ = SPACE$(40) THEN last$ = ""
    IF last$ <>"" THEN fullname$ = last$
    IF first$ <> "" THEN
    IF last$ <> "" THEN
    fullname$ = fullname$ + ", " + first
    ELSE
    fullname$ = first$
    END IF
    END IF



    ------------------
    Henning
    Henning

  • #2
    Henning:

    Try this...
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #INCLUDE "WIN32API.INC"
     
    TYPE Business_Table
       Company AS STRING * 40
       First   AS STRING * 40
       Last    AS STRING * 40
    END TYPE
     
    FUNCTION PBMAIN
       DIM BusList(1:100) AS Business_Table
     
       First$ = TRIM$(BusList(1).First, ANY CHR$(0) + CHR$(32))
       Last$  = TRIM$(BusList(1).Last,  ANY CHR$(0) + CHR$(32))
     
       IF Last$ <> "" THEN Fullname$ = Last$
       IF First$ <> "" THEN
          IF Last$ <> "" THEN
             Fullname$ = Fullname$ + ", " + First$
          ELSE
             Fullname$ = First$
          END IF
       END IF
     
       MSGBOX ">" + Fullname$ + "<  Length:" + STR$(LEN(Fullname$))
    END FUNCTION
    Timm
    mailto:[email protected]
    Tsunami Record Manager

    Comment


    • #3
      Hi Henning,

      Off the top of my head, try the following. I believe you need to
      use the "ANY" version of TRIM in order to get trim the Nulls (CHR$(0))
      from the type elements. By default strings in TYPEs are initialized
      to nulls.

      Code:
      first$ = TRIM$(BusList(i&).First, ANY CHR$(0,32)) 
      last$ = TRIM$(BusList(i&).Last, ANY CHR$(0,32)) 
      
      IF last$ <>"" THEN fullname$ = last$
         IF first$ <> "" THEN 
            IF last$ <> "" THEN 
            fullname$ = fullname$ + ", " + first
         ELSE
            fullname$ = first$
         END IF
      END IF

      ------------------
      Paul Squires
      www.PlanetSquires.com
      mailto:[email protected][email protected]</A>
      Paul Squires
      FireFly Visual Designer (for PowerBASIC Windows 10+)
      Version 3 now available.
      http://www.planetsquires.com

      Comment


      • #4
        Hey Timm, looks like we posted the answer at the exact same time!



        ------------------
        Paul Squires
        www.PlanetSquires.com
        mailto:[email protected][email protected]</A>
        Paul Squires
        FireFly Visual Designer (for PowerBASIC Windows 10+)
        Version 3 now available.
        http://www.planetsquires.com

        Comment


        • #5
          Paul:

          Depends on what time zone you're in... I'm CDT... how about you?
          mailto:[email protected]
          Tsunami Record Manager

          Comment


          • #6
            Henning,

            Now, I'm not sure if this is what you are looking for, but
            I think that if you used ASCIIZ type variables instead of a
            fixed length string...

            TYPE Business_Table
            Company AS ASCIIZ * 41
            First AS ASCIIZ * 41
            Last AS ASCIIZ * 41
            END TYPE

            FUNCTION PBMAIN AS LONG
            DIM BusList(1:100) AS Business_Table

            first$ = TRIM$(BusList(i&).First) ' : IF first$ = SPACE$(40) THEN first$ = ""
            last$ = TRIM$(BusList(i&).Last) ' : IF last$ = SPACE$(40) THEN last$ = ""

            MSGBOX STR$(LEN(first$))

            IF last$ <>"" THEN fullname$ = last$
            IF first$ <> "" THEN
            IF last$ <> "" THEN
            fullname$ = fullname$ + ", " + first$
            ELSE
            fullname$ = first$
            END IF
            END IF

            END FUNCTION

            Try changing the contents of first$, the msgbox should tell
            you the actual length. (0 if it is empty)

            ASCIIZ variables are fixed-length strings where the last character
            is always a null (0), so for a 40 character string it had to be defined
            using 41 bytes.

            Hope this helps,

            Neil Bertz

            ------------------
            Neil Bertz
            [email protected]
            PowerBASIC Staff

            [This message has been edited by Neil Bertz (edited October 18, 2001).]

            Comment


            • #7
              Code:
               If Len(Trim$(BusList(i&).First,any Chr$(0,32)) & _
                      Trim$(BusList(i&).Last ,any Chr$(0,32)))= 0 Then Struct is empty
              ------------------
              Fred
              mailto:[email protected][email protected]</A>
              http://www.oxenby.se

              Fred
              mailto:[email protected][email protected]</A>
              http://www.oxenby.se

              Comment

              Working...
              X