dbf to array

Post Reply
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

dbf to array

Post by Otto »

What is the best way to fill an array from a dbf-file (a kind of recordset).

Thanks in advance
Otto
demont frank
Posts: 167
Joined: Thu Mar 22, 2007 11:24 am

Post by demont frank »

I am using :

Code: Select all

# include "Common.ch"
# DEFINE MAAKBLCK(cvar)               &("{||"+cvar+"}")
# DEFINE MAAKINDBLCK(var,indexorde)   var := &("{||"+INDEXKEY(indexorde)+"}")
*********************************************************************************************************
PROC ReadDbf(Arr,ArrColBlock,cFilter, uFrom , uTo , cOrder  )
***************************************************************
// ArrColBlock may be nil (all fields are selected) , array from fieldnames or Codeblocks
LOCAL bFilt , IndBlck , HlpArr[0] , el
LOCAL j
LOCAL GehOrd
LOCAL Hlp
LOCAL aStruct := DbStruct() , aFields[0]
Aeval(aStruct , {|x|AADD(aFields,x[1])})
CursorWait()
Arr := {}
IF IsCharacter(cFilter)
	bFilt := MAAKBLCK(cFilter)
ELSEIF IsBlock(cFilter)
	bFilt := cFilter
END
IF IsNil(ArrColBlock)  .OR. ! IsArray(ArrColBlock)
	ArrColBlock := {}
	FOR j := 1 TO FCOUNT()
		AADD(ArrColBlock,FIELDBLOCK(FIELD(j)))
	NEXT
END
FOR EACH el IN ArrColBlock
	IF IsCharacter(el) .AND. ASCAN(aFields, UPPER(el)) > 0
		el := FIELDBLOCK(el)
	END
	IF ! IsBlock(el)
		Msginfo("Wrong column " + LTRIM(STR(Hb_EnumIndex()))
		RETURN
	END
NEXT
IF (IsCharacter(cOrder) .OR. IsNumber(cOrder)) .AND. OrdSetFocus() <> cOrder
	GehOrd := OrdSetFocus(cOrder)
END
IF IndexOrd() > 0
	MAAKINDBLCK(IndBlck,indexord())
	IF ! IsNil(uFrom)
		IF IsBlock(uFrom)
			uFrom := EVAL(uFrom)
		END
		DBSEEK(uFrom,.T.)
	ELSE
		GO TOP
	END
	IF ! IsNil(uTo)
		IF IsBlock(uTo)
			uTo := EVAL(uTo)
		END
	END
ELSE
	IF ! IsNil(uFrom) .AND. IsNumber(uFrom)
		IndBlck := {||RECNO()}
		IF !( !IsNil(uTo) .AND. IsNumber(uTo) )
			uTo := LASTREC()
		END
		DBGOTO(uFrom)
	ELSE
		GO TOP
	END
END
DO WHIL ! EOF() .AND. IIF(! IsNil(uTo) .AND. IsBlock(IndBlck), EVAL(indBlck) <= uTo , .T. )
	IF ! IsBlock(bFilt) .OR. EVAL(bFilt)
		ASIZE(hlparr,0)
		FOR EACH el IN ArrColBlock
			hlp := EVAL(el)
			AADD(hlparr , Hlp) //EVAL(el:bData))
		NEXT
		AADD(arr,ACLONE(hlparr))
	END
	SKIP
ENDD
IF ! IsNil(GehOrd)
	OrdSetFocus(GehOrd)
END
CursorArrow()
RETURN

User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: dbf to array

Post by Enrico Maria Giordano »

Code: Select all

FUNCTION MAIN()

    LOCAL aArray := {}

    LOCAL i

    USE TEST

    WHILE !EOF()
        AADD( aArray, {} )

        FOR i = 1 TO FCOUNT()
            AADD( ATAIL( aArray ), FIELDGET( i ) )
        NEXT

        SKIP
    ENDDO

    CLOSE

    // here you have the filled array

    RETURN NIL
EMG
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: dbf to array

Post by Enrico Maria Giordano »

But if you really want a sort of recordset then you should create a class.

EMG
Post Reply