Page 1 of 1
Saving an array
Posted: Fri Nov 30, 2007 9:42 pm
by TimStone
I can write a function to do this, but why re-invent the wheel if it exists.
Is there a function to save the data contained in the elements of an array to a database field ? I'm using DBFCDX and ADS.
Posted: Fri Nov 30, 2007 11:45 pm
by James Bott
Tim,
Here is one. I haven't tried it.
Regards,
James
Code: Select all
From: "Luis Krause Mantilla" <luis_krause@hotmail.com>
Subject: Re: Flexfile III
Date: Wednesday, December 22, 2004 6:19 PM
Colin
The 32 dll's (or whatever it was) from FF3 are unfinished and don't
work. I spend several days trying to make it work, but I realized
I was just wasting my time.
Because I only stored text and arrays in FF2 (I didn't use FF3),
DBFCDX or ADS will work just fine.
For storing arrays in xHarbour, usind ADS, I just created a function
to "stringify" the array before storing it in the memo field, and
a simple macro evaluation from the memo to get the array back.
If you use the V_xxx() functions, you'll also have to work your
way around this, but it won't be that difficult.
So far it's been more than I year since we've been using our app
with xHarbour + ADSCDX and we haven't missed FF.
This is the function to convert an array to a string (will work
with nested arrays)
* ÉÍ͹ Funci¢n ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
* º Nombre : wfAToC() º
* º Autor : C.P. Luis Krause Mantilla º
* º Objetivo : Convierte Arreglo a Cadena para almacenar en campos memo º
* º Fecha : 31/07/2003 º
* º Hora : 17:38:52 º
* ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĶ
* º Argumentos : aArray º
* ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĶ
* º Retorna : cArray º
* ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
Function wfAToC( aArray )
Local nI, nJ, cArray, nLen := Len( aArray ), cType, cString
cArray := "{"
For nI := 1 To nLen
cType := ValType( aArray[nI] )
If cType == "C" .or. cType == "M"
cString := aArray[nI]
// Considerar que la doble comilla puede ser parte de la cadena
Do While ( nJ := At( '"', cString ) ) > 0
cArray += '"' + SubStr( cString, 1, nJ - 1 ) + '"+["]+'
cString := SubStr( cString, nJ + 1 )
Enddo
cArray += '"' + cString + '"'
Elseif cType == "N"
cArray += LTrim( Str( aArray[nI] ) )
Elseif cType == "D"
cArray += "SToD(" + DToS( aArray[nI] ) + ")"
Elseif cType == "L"
cArray += If( aArray[nI], ".T.", ".F." )
Elseif cType == "A" // llamada recursiva para
cArray += wfAToC( aArray[nI] ) // procesar arreglos anidados
#ifdef __XHARBOUR__
Elseif cType == "P" // Pointer - s¢lo xHarbour
cArray += HB_NumToHex( aArray[nI] )
Elseif cType == "H" // Hash - s¢lo xHarbour; llamada recursiva para
cArray += wfAToC( aArray[nI] ) // procesar hashes anidados
#endif
Else // objects & codeblocks can't be stored anyway
cArray += "NIL"
Endif
If nI < nLen
cArray += ","
Endif
Next
cArray += "}"
Return cArray
This is the one that retrieves the array
* ÉÍ͹ Funci¢n ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
* º Nombre : wfCToA() º
* º Autor : C.P. Luis Krause Mantilla º
* º Objetivo : Convierte Cadena a Arreglo para restaurar de campos memo º
* º Fecha : 31/07/2003 º
* º Hora : 17:38:52 º
* ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĶ
* º Argumentos : cArray º
* ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĶ
* º Retorna : aArray º
* ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
Function wfCToA( cArray )
Local aArray, bArray
If wfCompile( cArray, .F., @bArray ) // no queremos mensaje si truena
aArray := Eval( bArray )
Endif
If ! ValType( aArray ) == "A" // par metro no proporcionado o
aArray := {} // error al intentar convertir
Endif
Return aArray
Replace wfCompile() for you function that will test and trap
a possible error, i.e. Eval( &( "{||" + cArray + "}" ) )
HTH.
Colin Wisbey wrote:
> I am a FW (16 bit) user and have delayed going 32 bit due to extensive use
> of Flexfile functions in all my current apps. I love Flexfile. ADS doesn't
> suit my needs and I want to avoid using traditional memo fields if at all
> possible.
>
> I had the impression that Flexfile was not available for 32 bit but I just
> discovered that GrafX currently sell Flexfile III, including 32 bit.
>
> Is anyone using it with xHarbour? If so, I'd be grateful for any feedback on
> reliability etc in the 32 bit environment.
>
> (I've already emailed Grafx too and am awaiting their reply)
>
> TIA
> Colin Wisbey
>
>
--
Luis Krause Mantilla
lkrausem at shaw dot ca
luis_krause at hotmail dot com
"May the Source be with GNU"
Array save
Posted: Sat Dec 01, 2007 12:04 am
by TimStone
Encoding is the way I anticipated. I'm a bit puzzled by the decoding logic ... and Replace comment.
Posted: Sat Dec 01, 2007 12:15 am
by James Bott
Tim,
Here is another string to array function. It appears that this only works with single dimension arrays though.
James
Code: Select all
// Convert a string to an array
// cSeparator is the separator character, defaults to TAB
function string2array(cString,cSeparator)
local nCount:=1,aArray:={},cToken:=""
default cSeparator:=chr(9)
do while .t.
cToken:= strToken(cString,nCount,cSeparator)
if ! empty(cToken)
aadd(aArray,cToken)
nCount++
else
exit
endif
enddo
return aArray
Array
Posted: Sat Dec 01, 2007 12:25 am
by TimStone
I'm only using single dimensional arrays with the module I'm working on. I was going to store the array elements plus separators to a string, and then save that value to a memo field. Since it is only a list, one dimension is all that is needed, and it will be only character data. Of course I could see expanding the capability, needing to do multi-dimensional arrays, plus numeric and logical values, so I'll probably "build it strong" to start !
Posted: Sat Dec 01, 2007 12:29 am
by James Bott
Tim,
I just found this reference in my notes too. You might want to check into it also.
James
From: "Ron Pinkas" <
Ron@remove-this.xharbour.com>
Subject: Re: Array in memo field?
Date: Thursday, December 02, 2004 9:13 AM
> Is it possible write an array in a memo field with xHarbour or
> Harbour?
Yes, xHarbour has ValToPrg() and ValToPrgExp() which will save ANY TYPE
(Even Objects) into a String.
Ron
Arrays
Posted: Sat Dec 01, 2007 12:42 am
by TimStone
I'll have to play with that. You can use ValToPrgExp( ), and then reverse the result with PrgExpToVal( ).
I did a search on the xHarbour newsgroup, and a search of the docs, but apparently didn't have the keywords to find those.
Thanks.
Is it wet enough yet ?
Posted: Sat Dec 01, 2007 12:51 am
by James Bott
Tim,
> You can use ValToPrgExp( ), and then reverse the result with PrgExpToVal( ).
I'm putting this in my notes. Keep us posted on your progress with this.
>Is it wet enough yet ?
Judging from the water in the cat food dish on our patio, we have had over an inch so far today. Boy do we need it! The cats are not happy though...
James
Posted: Sat Dec 01, 2007 1:12 am
by Antonio Linares
Tim,
FWH provides:
ASave( aArray ) --> cText
ARead( cText ) --> aArray
Here you have a sample:
Code: Select all
function Main()
local aInfo := { { "one", 1, .T. }, { "two", 2, .F. }, { "three", 3, Date() } }
local cText := ASave( aInfo )
aInfo := nil
aInfo = ARead( cText )
MsgInfo( Len( aInfo ) )
MsgInfo( aInfo[ 1 ][ 1 ] )
MsgInfo( aInfo[ 3 ][ 3 ] )
return nil
Posted: Sat Dec 01, 2007 1:16 am
by James Bott
Antonio,
ASave(), ARead()
I am wondering if these will handle multidimensional arrays too? What about numeric and logical arrays?
James
Posted: Sat Dec 01, 2007 1:18 am
by Antonio Linares
James,
Yes, they manage all of them. The only limitation is nested arrays.
See the above little working sample
ASave / ARead
Posted: Sat Dec 01, 2007 1:23 am
by TimStone
OK Antonio ... you're working late again ... go hang with your family !
James and I are getting our first real rain in quite awhile ... and hopefully it will be short enough to not cause mud slides in the fire areas !
I looked at ASave and ARead in the docs, but I thought it meant it put the names of the arrays into memory, not the values of the array elements.
Parameters:
<aArray> Array to code.
Returns:
<cInfo> String containing the coded array.
Thanks for the sample. I asked the question after reading that.
Posted: Sat Dec 01, 2007 1:24 am
by James Bott
Antonio,
Nice. Thanks for pointing those functions out and the example.
Tim, it looks like these functions are all you need.
James
Posted: Sat Dec 01, 2007 1:27 am
by Antonio Linares
Tim,
> OK Antonio ... you're working late again ...
You are right
Time to go to bed. Good night my friends