ARRAY BY : SITI MARIYAH, SST ARRAY Contoh array 1 dimensi: kelasku(1,5) = {“1A”, “1B”, “1C”, “1D”, “1E”} Data 5 kelas dimasukkan dalam satu array yang bernama kelas. Contoh array 2 dimensi : kelasku(2, 3) = {(“1A”, “1B”, “1C”),(“1E”, “1F”, “1G”)} DIMENSION COMMAND Logic : DIMENSION ArrayName1(nRows1 [, nColumns1]) [AS Type] [, ArrayName2(nRows2 [,nColumns2])] PARAMETER 1. ArrayName1 nama array Kita bisa membuat multiple array dengan satu dimensi hanya dengan menambahkan nama array. Contoh : DIMENSION arrayName..,arrayName2…,arrayName3… 2. nRows1 [,nColumns1] jumlah baris dan kolom array Contoh : DIMENSION arraySatu(10) array 1 dimensi dengan 10 baris dan 1 kolom DIMENSION arraySatu(2,4) array 2 dimensi dengan 2 baris dan 4 kolom DEKLARASI ARRAY Contoh deklarasi : DIMENSION arraySatu(10), arrayDua[2,4], arrayTiga(3,3) DIMENSION arraySatu[10], arrayDua(2,4), arrayTiga[3,3] Ukuran array tergantung pada banyaknya elemen yang disimpan dalam array tersebut. Elemen array dapat memiliki tipe data apa saja dan diinisialisasi False (.F.) untuk pertama kali. OPERASI ARRAY STORE TO : Untuk menginisialisasi semua elemen dengan nilai yang sama. Contoh : DIMENSION arraySatu(10,3) STORE “initial” TO arraySatu Logic di atas untuk menyimpan string initial ke arraySatu. Sehingga initial menjadi elemen pertama (baris1,kolom1) arraySatu OPERASI ARRAY(2) Array Subscript : digunakan untuk mengakses dan manipulasi elemen array. • Selain array subscript bisa juga menggunakan nama array dan indeks. • Pada array 1 dimensi, subscript digunakan untuk mengidentifikasi nomor baris array. Contoh, subscript untuk elemen yang berada di baris ketiga adalah 3 • Pada array 2 dimensi, subscript digunakan untuk mengidentifikasi elemen array. Subscript yang pertama menyatakan baris, subscript kedua menyatakan kolom DIMENSION arrayName[5,2] arrayName[1,2] = 966789 • Creates a one- or two-dimensional array. • Visual FoxPro arrays are one-based • DIMENSION is identical in operation and similar in syntax to the DECLARE command • Copies elements from one array to another array. • Deletes an element from a one-dimensional array, or a row or column from a two-dimensional array. • Adds one record to the currently selected table for each row in an array and fills each record with data from the corresponding array row. • Copies data from the currently selected table to an array. • Executes the first set of commands whose conditional expression evaluates to true (.T.). Contoh STORE CMONTH(DATE()) TO month DO CASE CASE INLIST(month, 'January', 'February', 'March') STORE 'first quarter' TO rpt_title CASE INLIST(month, ‘April', ‘May', ‘June') STORE ‘second quarter' TO rpt_title CASE INLIST(month, 'July', ‘August', ‘September') STORE ‘third quarter' TO rpt_title OTHERWISE STORE ‘fourth quarter' TO rpt_title ENDCASE WAIT WINDOW rpt_title NOWAIT • Executes a set of commands within a conditional loop. • Exits a DO WHILE, FOR, SCAN, or TRY…CATCH…FINALLY structure. Contoh CLOSE DATABASES OPEN DATABASE (HOME(2) + 'Data\testdata') USE products && Opens Products table SET TALK OFF gnStockTot = 0 DO WHILE .T. && Begins loop IF EOF( ) EXIT ENDIF IF unit_price < 20 SKIP LOOP ENDIF gnStockTot = gnStockTot + in_stock SKIP ENDDO && Ends loop CLEAR ? 'Total items in stock valued over 20 dollars:' ?? gnStockTot • Executes a set of commands for each element in a Visual FoxPro array or collection. Contoh DIMENSION cMyArray(3) cMyArray[1] = 'A' cMyArray[2] = 'B' cMyArray[3] = 'C' FOR EACH cMyVar IN cMyArray ? cMyVar ENDFOR • Executes a set of commands a specified number of times. Contoh FOR gnCount = 1 TO 10 ? gnCount ENDFOR Contoh OPEN DATABASE (HOME(2) + 'Data\TestData') USE Customer FOR gnCount = 1 TO 10 STEP 2 GOTO gnCount DISPLAY Company ENDFOR • Conditionally executes a set of commands based on the value of a logical expression. Contoh USE Customer && Open customer table GETEXPR ‘ Enter condition to locate ' TO gcTemp; TYPE 'L' DEFAULT ‘ COMPANY = ""' LOCATE FOR &gcTemp && Enter LOCATE expression IF FOUND( ) && Was it found? DISPLAY && If so, display the record ELSE && If not found ? 'Condition ' + gcTemp + ' was not found ' &&Display a message ENDIF • Returns program control directly to the beginning of a looping structure. • Moves the record pointer through the currently selected table and executes a block of commands for each record that meets the specified conditions.