Purpose Read a random line of a text file This is useful when you want to generate test data via GuiXT. Create a set of files with customer numbers, material numbers and so on and then carry out SAP transactions with data that you pick randomly from these base files. |
Solution Use the current time as 'seed' of a pseudo-random algorithm, see the include "read_random_record.txt" below.
Example InputScript // Test script which displays 9 random
lines Set V[k] 1Clear text[msg] label next_line// read a random line Set V[RETURNLABEL] "random_line_read" goto read_random_record label random_line_read CopyText fromString="LINE" toText="msg" -appendLineSet V[k] &V[k] + 1if V[k<10] goto next_line endif // Display random lines Return // Read random line in given text file
Remarks
label read_random_recordSet V[LINE] ""OpenFile "&V[FILENAME]" -noDelimiter if not Q[ok] goto "&V[RETURNLABEL]" endif Set V[MAXRECORDS] 1label random_read_file_1ReadFile "&V[FILENAME]" "LINE"if Q[ok] Set V[MAXRECORDS] &V[MAXRECORDS] + 1 goto random_read_file_1 endif Set V[MAXRECORDS] &V[MAXRECORDS] - 1CloseFile "&V[FILENAME]" if V[MAXRECORDS=0]goto "&V[RETURNLABEL]" endif // next random number Set V[RANDOM] "&V[today_hms]" // seed endif Set
V[RANDOM]
&V[RANDOM]
* 16807 Set V[M] &V[RANDOM] / &V[MAXRECORDS] decimals=0Set V[M] &V[M] * &V[MAXRECORDS] Set V[M] &V[RANDOM] - &V[M] if V[M<1] Set V[M] &V[MAXRECORDS] + &V[M] endif // read file, line M if not Q[ok] goto "&V[RETURNLABEL]" endif Set V[K] 1label random_read_file ReadFile "&V[FILENAME]" "LINE" if Q[ok] and V[K<&V[M]] Set V[K] &V[K] + 1 goto random_read_file endif CloseFile "&V[FILENAME]"goto "&V[RETURNLABEL]"
|
Components |