250 likes | 499 Views
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.
E N D
ARRAY BY : SITI MARIYAH, SST
ARRAY Contoh array 1 dimensi: kelasku(1,5) = {“1A”, “1B”, “1C”, “1D”, “1E”} Data 5 kelasdimasukkandalamsatu array yang bernamakelas. 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 • ArrayName1 namaarray Kita bisamembuat multiple array dengansatudimensihanyadenganmenambahkannama array. Contoh: DIMENSION arrayName..,arrayName2…,arrayName3… • nRows1 [,nColumns1] jumlahbarisdankolom array Contoh: DIMENSION arraySatu(10) array 1 dimensidengan 10 barisdan 1 kolom DIMENSION arraySatu(2,4) array 2 dimensidengan 2 barisdan 4 kolom
DEKLARASI ARRAY Contohdeklarasi : DIMENSION arraySatu(10), arrayDua[2,4], arrayTiga(3,3) DIMENSION arraySatu[10], arrayDua(2,4), arrayTiga[3,3] Ukuran array tergantungpadabanyaknyaelemen yang disimpandalam array tersebut. Elemen array dapatmemilikitipe data apasajadandiinisialisasi False (.F.) untukpertama kali.
OPERASI ARRAY STORE TO : Untukmenginisialisasisemuaelemendengannilaiyang sama. Contoh: DIMENSION arraySatu(10,3) STORE “initial” TO arraySatu Logic di atasuntukmenyimpan string initial kearraySatu. Sehingga initial menjadielemenpertama (baris1,kolom1) arraySatu
OPERASI ARRAY(2) Array Subscript : digunakanuntukmengaksesdanmanipulasielemen array. • Selain array subscript bisajugamenggunakannama array danindeks. • Pada array 1 dimensi, subscript digunakanuntukmengidentifikasinomorbaris array. Contoh, subscript untukelemen yang berada di barisketigaadalah 3 • Pada array 2 dimensi, subscript digunakanuntukmengidentifikasielemen array. Subscript yang pertamamenyatakanbaris, subscript keduamenyatakankolom DIMENSION arrayName[5,2] arrayName[1,2] = 966789
DECLARE Command • Creates a one- or two-dimensional array. • Visual FoxPro arrays are one-based
DIMENSION Command • DIMENSION is identical in operation and similar in syntax to the DECLARE command
ACOPY( ) Function • Copies elements from one array to another array.
ADEL( ) Function • Deletes an element from a one-dimensional array, or a row or column from a two-dimensional array.
APPEND FROM ARRAY Command • 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.
COPY TO ARRAY Command • Copies data from the currently selected table to an array.
DO CASE ... ENDCASE Command • 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
DO WHILE ... ENDDO Command • Executes a set of commands within a conditional loop. EXIT Command • 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
FOR EACH ... ENDFOR Command • 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
FOR ... ENDFOR Command • 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
IF ... ENDIF Command • 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
LOOP Command • Returns program control directly to the beginning of a looping structure.
SCAN ... ENDSCAN Command • Moves the record pointer through the currently selected table and executes a block of commands for each record that meets the specified conditions.