Array to String?

194 views
Skip to first unread message

axtens

unread,
May 13, 2024, 7:33:34 PM5/13/24
to Harbour Users
I was sure there is a Harbour function to convert array to string with separator but I can't find it. Am I imagining things? I've looked for functions like AJOIN, ARR2STR and many others. What's the name of it?

-Bruce

slar...@gmail.com

unread,
May 13, 2024, 8:48:02 PM5/13/24
to Harbour Users
FUNCTION aToStr( a AS ARRAY )
   LOCAL cVar AS STRING

   aEval( a, {  | e, n | cVar += e + "," ) }

   cVar := SubStr( cVar, 1, Len(cVar)-1 )

RETURN( cVar )


Not tested but it should give you what you need

Sylvain Larche
Consultant

axtens

unread,
May 13, 2024, 9:23:56 PM5/13/24
to Harbour Users
Thank you!!

-Bruce

axtens

unread,
May 13, 2024, 10:14:36 PM5/13/24
to Harbour Users
What I ended up with

procedure main()
    LOCAL x := array(10), i
    FOR i := 1 TO LEN( x )
        x[i] = i * i
        if i = 4
            x[i] = "dog"
        endif
    NEXT
    ? AJOIN(x,"-")
RETURN

FUNCTION AJOIN( arr AS ARRAY, separator AS STRING )
    LOCAL cVar AS STRING := "", i
   
    IF PCOUNT() < 2 .OR. separator == NIL
        separator := ","
    ENDIF
   
    IF LEN( arr ) = 0
        RETURN ""
    ENDIF
   
    cVar = ALLTRIM(TRANSFORM( arr[1], "@A" ))
   
    FOR i := 2 TO LEN( arr )
        cVar += separator + ALLTRIM(TRANSFORM( arr[i], "@A" ))
    NEXT
   
    RETURN cVar

nbato...@wings.rs

unread,
May 14, 2024, 9:41:56 AM5/14/24
to harbou...@googlegroups.com

FUNC VarStr (xVal, lString, cPic)

   LOCAL i, xType, cTmp

 

   DEFAULT lString TO FALSE

 

   xType := ValType(xVal)

 

   DO CASE

 

      CASE xType == "N"

         IF cPic == NIL

            cTmp := Str(xVal)

         ELSE

            cTmp := Transform (xVal, cPic)

         END IF

 

      CASE xType == "C" .AND.  ! ('"' $ xVal)

         IF lString

            cTmp := xVal

         ELSE

            cTmp := '"' + xVal + '"'

         END IF

 

      CASE xType == "C" .AND. ! ("'" $ xVal)

         IF lString

            cTmp := xVal

         ELSE

            cTmp := "'" + xVal + "'"

         END IF

 

      CASE xType == "C"

         IF lString

            cTmp := xVal

         ELSE

            cTmp := '[' + xVal + ']'

         END IF

 

      CASE xType == "D"

         IF lString

            cTmp := DtoC(xVal)

         ELSE

            cTmp := "CtoD([" + DtoC(xVal) + "])"

         END IF

 

      CASE xType == "L"

         cTmp := IIF (xVal, ".T.", ".F.")

 

      CASE xType == "A"

         cTmp := "{"

         FOR i := 1 TO Len(xVal)

            c := xVal[i]

            cTmp += VarStr(xVal[i]) + IIF (i <> Len(xVal), ",", "")

         NEXT

         cTmp += "}"

 

      OTHERWISE

         cTmp := ""

 

   END CASE

RETURN AllTrim(cTmp)

 

 

Damian S

unread,
May 14, 2024, 9:51:54 AM5/14/24
to Harbour Users
Hi.
hb_ValToExp() or hb_ValToStr()
Regards, Damian

wen....@gmail.com

unread,
May 22, 2024, 11:25:50 AM5/22/24
to Harbour Users
You can refer to the URL:
https://www4.zzz.com.tw/phpBB3/viewtopic.php?f=2&t=360

Supports nested arrays.
Damian S 在 2024年5月14日 星期二晚上9:51:54 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages