Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Cursor from array
Message
De
15/12/2007 07:06:37
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01276020
Message ID:
01276079
Vues:
29
How can I create a cursor from a multidimensional array ?
********************************************************************
*** Name.....: ARRAY2CURSOR.PRG
*** Author...: Andy Kramek & Marcia G Akins
*** Date.....: 29/09/2003
*** Notice...: Copyright (c) 2003 Tightline Computers, Inc
*** Compiler.: Visual FoxPro 08.00.0000.3117 for Windows 
*** Function.: Convert the contents of the specified array into a cursor for easy viewing
*** Returns..: Number of Rows in the cursor
********************************************************************
LPARAMETERS taSceArray, tcCursorName
LOCAL lcCursor, lnRows, lnCols, lnCnt, lnColSize, lnRowCnt
LOCAL ARRAY laStru[1]
EXTERNAL ARRAY taSceArray

*** Check that we have an array as a parameter
*** NB Cannot use VarType() here in case array does NOT exist!
IF TYPE( "taSceArray[1]" ) = "U"
  ASSERT .F. MESSAGE "Must pass a valid Array to ArrayToCursor()"
  RETURN 0
ENDIF

*** Default Cursor Name to "arraycur" if nothing passed
lcCursor = IIF( VARTYPE( tcCursorName ) = "C" AND ! EMPTY( tcCursorName ), ;
                ALLTRIM( LOWER( tcCursorName )), "arraycur" )

*** Determine the size of the array
lnRows = ALEN(taSceArray,1)
lnCols = MAX( ALEN(taSceArray,2), 1 )
*** And ensure it is dimensioned 'properly'
*** APPEND FROM ARRAY seems to require that the array be 
*** two-dimensional even if it has only one column
IF lnCols <= 1
  DIMENSION taSceArray[ lnRows, 1 ]
ENDIF

*** Create the structure array
DIMENSION laStru(lnCols, 4)

*** And determine the columns for the cursor
FOR lnCnt = 1 TO lnCols

  *** Determine Maximum Column width needed for each column
  lnColSize = 6
  FOR lnRowCnt = 1 TO lnRows
    lnColSize = MAX( lnColSize, LEN( TRANSFORM( taSceArray[lnRowCnt, lnCnt] )))
  ENDFOR

  *** Name Columns with "Col" + Column number
  laStru[ lnCnt, 1 ] = "Col" + TRANSFORM( lnCnt )  && Name
  laStru[ lnCnt, 2 ] = "C"       && Data Type
  laStru[ lnCnt, 3 ] = lnColSize && Col Width
  laStru[ lnCnt, 4 ] = 0         && No Decimals
ENDFOR

*** Create the cursor from the structure array
CREATE CURSOR (lcCursor) FROM ARRAY laStru

*** And populate it
APPEND FROM ARRAY taSceArray
GO TOP IN (lcCursor)
  
*** Return Number of records
RETURN RECCOUNT( lcCursor )
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform