Page 1 of 1

Generating a readable text string from an array

Posted: Fri Oct 14, 2022 5:00 pm
by HGAutomator
Still working on this, but need to break for a day or two.

Next TODO is to indent and CRLF some of the subarrays and/or elements, in a way that makes sense to the human eye.

This is different from many of the Array2File or Array2String modules, in that it's strictly textual and readable. At least, that's the goal.

In case anyone wants to take a look, this is a work in progress.

Code: Select all

#define CRLF CHR(10) + CHR(13)

STATIC MultiDimensionalArray_s := ""

Function ConvertArrayToString()
	Local ArrayString_s := ""
  Local SampleArray_a := ;
   { ;
      {"One", "Two", "Three", { "Four", "Five" } }, ;
      { "Six", "Seven" }, ;
      {"Eight", "Nine"} ;
   }

	SampleArray_a := ;
	{ ;
		{"One", "Two", "Three", { "Four", "Five" } }, ;
		{ { "Dummy1", "Dummy2" }, "Six", "Seven" }, ;
		{"Eight", "Nine"} ;
	}

  ArrayString_s := "{ " + CRLF + WriteArray( SampleArray_a, 1 ) + CRLF + " }"

	// Send ArrayString_s to file SAMPLEARRAY.TXT

	SET CONSOLE OFF
	SET PRINTER ON
	SET PRINTER TO SAMPLEARRAY.TXT
	? ArrayString_s
	SET PRINTER OFF
	SET PRINTER TO
	SET CONSOLE ON

Return Nil


FUNCTION WriteArray(aArray, Level_n )
LOCAL TypeOfElement_s := ""
LOCAL NthElement_n := 0
LOCAL LengthOfArray_n := 0

LengthOfArray_n := LEN(aArray)

MultiDimensionalArray_s := MultiDimensionalArray_s + Space( Level_n )

FOR NthElement_n = 1 TO LengthOfArray_n
   TypeOfElement_s = VALTYPE(aArray[NthElement_n])
   DO CASE
      CASE TypeOfElement_s = 'A'
         If NthElement_n == 1
            MultiDimensionalArray_s := MultiDimensionalArray_s + " { " + CRLF
         Else
            If NthElement_n < LengthOfArray_n
               MultiDimensionalArray_s := MultiDimensionalArray_s + " { "
            Else
               If NthElement_n == LengthOfArray_n
                  MultiDimensionalArray_s := MultiDimensionalArray_s + " { " + CRLF
               EndIf
            EndIf
         EndIf

         WriteArray(aArray[NthElement_n], Level_n + 1)

         IF ( NthElement_n == 1 .AND. NthElement_n < LengthOfArray_n )
            MultiDimensionalArray_s := MultiDimensionalArray_s + " },  " + CRLF
         Else
            If NthElement_n < LengthOfArray_n
               MultiDimensionalArray_s := MultiDimensionalArray_s + " }, "
            Else
               If NthElement_n == LengthOfArray_n
                  MultiDimensionalArray_s := MultiDimensionalArray_s + " } " + CRLF
               EndIf
            EndIf
         EndIf

      CASE TypeOfElement_s = 'C'
          If NthElement_n < LengthOfArray_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + aArray[NthElement_n] + ", "
         Else
            MultiDimensionalArray_s := MultiDimensionalArray_s + aArray[NthElement_n] + " "
         EndIf

      CASE TypeOfElement_s = 'D'
          MultiDimensionalArray_s := MultiDimensionalArray_s + DTOC(aArray[NthElement_n]) + " "

      CASE TypeOfElement_s = 'L'
          MultiDimensionalArray_s := MultiDimensionalArray_s + IF(aArray[NthElement_n], 'T', 'F') + " "

      CASE TypeOfElement_s = 'N'
          MultiDimensionalArray_s := MultiDimensionalArray_s + LTRIM(STR(aArray[NthElement_n] ) ) + " "
      OTHERWISE
          MultiDimensionalArray_s := MultiDimensionalArray_s + 'X' + " "
    ENDCASE
NEXT

RETURN MultiDimensionalArray_s

Re: Generating a readable text string from an array

Posted: Mon Oct 17, 2022 7:08 pm
by SALINETAS24
HGAutomator wrote: Fri Oct 14, 2022 5:00 pm Still working on this, but need to break for a day or two.

Next TODO is to indent and CRLF some of the subarrays and/or elements, in a way that makes sense to the human eye.

This is different from many of the Array2File or Array2String modules, in that it's strictly textual and readable. At least, that's the goal.

In case anyone wants to take a look, this is a work in progress.......


RETURN MultiDimensionalArray_s
[/code]
Hola, no tengo claro lo quieres hacer. Mi inglés es malo. :?:
No obstante te paso un programa que te convierte una ARRAY multi en una cadena de caracteres y que la muestra con los distintos niveles. Espero que te sirva para algo.
Saludos cordiales y cervezas virtuales .... :lol:

Re: Generating a readable text string from an array

Posted: Mon Oct 17, 2022 7:47 pm
by HGAutomator
Hi Salinet,

I haven't tested your code yet, but it does look good.

Will check it out in a few days, after catching up on other stuff.


Regards,



SALINETAS24 wrote: Mon Oct 17, 2022 7:08 pm
HGAutomator wrote: Fri Oct 14, 2022 5:00 pm Still working on this, but need to break for a day or two.

Next TODO is to indent and CRLF some of the subarrays and/or elements, in a way that makes sense to the human eye.

This is different from many of the Array2File or Array2String modules, in that it's strictly textual and readable. At least, that's the goal.

In case anyone wants to take a look, this is a work in progress.......


RETURN MultiDimensionalArray_s
[/code]
Hola, no tengo claro lo quieres hacer. Mi inglés es malo. :?:
No obstante te paso un programa que te convierte una ARRAY multi en una cadena de caracteres y que la muestra con los distintos niveles. Espero que te sirva para algo.
Saludos cordiales y cervezas virtuales .... :lol:

Re: Generating a readable text string from an array

Posted: Wed Oct 19, 2022 7:08 pm
by HGAutomator
Salinet,

Not quite. Your code lines up the elements of the array nicely, but doesn't generate the braces.

Still busy, haven't had a chance to do any more coding this week so far. But the resultant string has to look something like this:


{;
{"One", "Two", "Three",
{ "Four", "Five" } }, ;
{
{ "Dummy1", "Dummy2" },
"Six", "Seven" }, ;
{"Eight", "Nine"} ;
}

Anyway, I'll share when it's done, thanks.

Re: Generating a readable text string from an array

Posted: Wed Oct 19, 2022 10:24 pm
by HGAutomator
Not finished yet, but some progress:

Code: Select all

STATIC FUNCTION WriteArray( _a, Level_n )
   LOCAL ElementType_s, NthElement_n, ArrayLength_n := Len( _a )

   MultiDimensionalArray_s := MultiDimensionalArray_s + Space( Level_n )

   FOR NthElement_n = 1 TO ArrayLength_n
      ElementType_s = ValType( _a[ NthElement_n ] )
      DO CASE
      CASE ElementType_s = 'A'
         IF NthElement_n == 1
            MultiDimensionalArray_s := MultiDimensionalArray_s + "{ " + CRLF
         ELSE
            IF NthElement_n < ArrayLength_n
               MultiDimensionalArray_s := MultiDimensionalArray_s + "{ "
            ELSE
               IF NthElement_n == ArrayLength_n
                  MultiDimensionalArray_s := MultiDimensionalArray_s + "{ " + CRLF
               ENDIF
            ENDIF
         ENDIF

         WriteArray( _a[ NthElement_n ], Level_n + 1 )

         IF NthElement_n == 1 .AND. NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + CRLF + " }, "
         ELSE
            IF NthElement_n < ArrayLength_n
               MultiDimensionalArray_s := MultiDimensionalArray_s + " }, "
            ELSE
               IF NthElement_n == ArrayLength_n
                  MultiDimensionalArray_s := MultiDimensionalArray_s + CRLF +  " }"
               ENDIF
            ENDIF
         ENDIF

      CASE ElementType_s = 'C'
         OutDebug( _a[ NthElement_n ] )
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + Chr(34) + _a[ NthElement_n ] + Chr(34) + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + Chr(34) + _a[ NthElement_n ] + Chr(34) +  " "
         ENDIF

      CASE ElementType_s = 'D'
         OutDebug( DToC( _a[ NthElement_n ] ) )
         MultiDimensionalArray_s := MultiDimensionalArray_s + DToC( _a[ NthElement_n ] ) + " "

      CASE ElementType_s = 'L'
         OutDebug( IF( _a[ NthElement_n ], 'T', 'F' ) )
         MultiDimensionalArray_s := MultiDimensionalArray_s + IF( _a[ NthElement_n ], 'T', 'F' ) + " "

      CASE ElementType_s = 'N'
         OutDebug( LTrim( Str(_a[ NthElement_n ] ) ) )
         MultiDimensionalArray_s := MultiDimensionalArray_s + LTrim( Str( _a[ NthElement_n ] ) ) + " "

      CASE ElementType_s = 'B'
         OutDebug( LTrim( Str(_a[ NthElement_n ] ) ) )
         MultiDimensionalArray_s := MultiDimensionalArray_s + 'B' + " "

      OTHERWISE
         OutDebug( 'X' )
         MultiDimensionalArray_s := MultiDimensionalArray_s + 'X' + " "
      ENDCASE
   NEXT

RETURN MultiDimensionalArray_s

Re: Generating a readable text string from an array

Posted: Wed Oct 19, 2022 10:41 pm
by HGAutomator
Much closer, to what we're trying to accomplish:

Code: Select all

   SampleArray_a := ;
      { ;
      { "One", "Two", "Three", { "Four", "Five" } }, ;
      { { "Dummy1", "Dummy2" }, "Six", "Seven" }, ;
      { "Eight", "Nine", Date(), 123, { || dummyfunc() } }, ;
      { .F., .T. } ;
      }
      
   ArrayString_s := "{ " + CRLF + WriteArray( SampleArray_a, 1 ) + CRLF + "}"

FUNCTION WriteArray( _a, Level_n )
   LOCAL ElementType_s, NthElement_n, ArrayLength_n := Len( _a )

   MultiDimensionalArray_s := MultiDimensionalArray_s + Space( Level_n )

   FOR NthElement_n = 1 TO ArrayLength_n
      ElementType_s = ValType( _a[ NthElement_n ] )
      DO CASE
      CASE ElementType_s = 'A'
         IF NthElement_n == 1
            MultiDimensionalArray_s := MultiDimensionalArray_s + "{ " + CRLF
         ELSE
            IF NthElement_n < ArrayLength_n
               MultiDimensionalArray_s := MultiDimensionalArray_s + "{ "
            ELSE
               IF NthElement_n == ArrayLength_n
                  MultiDimensionalArray_s := MultiDimensionalArray_s + "{ " + CRLF
               ENDIF
            ENDIF
         ENDIF

         WriteArray( _a[ NthElement_n ], Level_n + 1 )

         IF NthElement_n == 1 .AND. NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + CRLF + " }, "
         ELSE
            IF NthElement_n < ArrayLength_n
               MultiDimensionalArray_s := MultiDimensionalArray_s + " }, "
            ELSE
               IF NthElement_n == ArrayLength_n
                  MultiDimensionalArray_s := MultiDimensionalArray_s + CRLF +  " }"
               ENDIF
            ENDIF
         ENDIF

      CASE ElementType_s = 'C'
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + Chr(34) + _a[ NthElement_n ] + Chr(34) + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + Chr(34) + _a[ NthElement_n ] + Chr(34) +  " "
         ENDIF

      CASE ElementType_s = 'D'
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + DToC( _a[ NthElement_n ] ) + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + DToC( _a[ NthElement_n ] ) + " "
         ENDIF

      CASE ElementType_s = 'L'
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + IF( _a[ NthElement_n ], 'T', 'F' ) + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + IF( _a[ NthElement_n ], 'T', 'F' ) + " "
         ENDIF

      CASE ElementType_s = 'N'
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + LTrim( Str( _a[ NthElement_n ] ) ) + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + LTrim( Str( _a[ NthElement_n ] ) ) + " "
         ENDIF

      CASE ElementType_s = 'B'
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + 'B' + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + 'B' + " "
         ENDIF
      OTHERWISE
         IF NthElement_n < ArrayLength_n
            MultiDimensionalArray_s := MultiDimensionalArray_s + 'X' + ", "
         ELSE
            MultiDimensionalArray_s := MultiDimensionalArray_s + 'X' + " "
         ENDIF


      ENDCASE
   NEXT

RETURN MultiDimensionalArray_s


So, for the sample array

Code: Select all

      { ;
      { "One", "Two", "Three", { "Four", "Five" } }, ;
      { { "Dummy1", "Dummy2" }, "Six", "Seven" }, ;
      { "Eight", "Nine", Date(), 123, { || dummyfunc() } }, ;
      { .F., .T. } ;
      }

, the function generates the string

Code: Select all

{ 
 { 
  "One", "Two", "Three", { 
   "Four", "Five" 
 }
 }, {   { 
   "Dummy1", "Dummy2" 
 }, "Six", "Seven"  }, {   "Eight", "Nine", 10/19/22, 123, B  }, { 
  F, T 
 }
}

Re: Generating a readable text string from an array

Posted: Thu Oct 20, 2022 6:51 pm
by SALINETAS24
HGAutomator wrote: Wed Oct 19, 2022 10:41 pm Much closer, to what we're trying to accomplish: .....
Hola HGAutomator, después de unos ajustes este es el resulado

Code: Select all

{
{"One", "Two", "Three", 
{ "Four", "Five" } }, 

{ 
{ "Dummy1", "Dummy2" }, 
"Six", "Seven" }, ;
{"Eight", "Nine",10/20/22,123,B} ,
{F, T}
}
Vamos con una cervecita fresquita para todo el personal.....

Re: Generating a readable text string from an array

Posted: Thu Oct 20, 2022 7:00 pm
by HGAutomator
Yep, that seems to work.

SALINETAS24 wrote: Thu Oct 20, 2022 6:51 pm
HGAutomator wrote: Wed Oct 19, 2022 10:41 pm Much closer, to what we're trying to accomplish: .....
Hola HGAutomator, después de unos ajustes este es el resulado

Code: Select all

{
{"One", "Two", "Three", 
{ "Four", "Five" } }, 

{ 
{ "Dummy1", "Dummy2" }, 
"Six", "Seven" }, ;
{"Eight", "Nine",10/20/22,123,B} ,
{F, T}
}
Vamos con una cervecita fresquita para todo el personal.....