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
W
e use the following sample file:

InputScript

// Test script which displays 9 random lines
Set V[FILENAME] "names.txt"

Set V[k] 1
Clear 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" -appendLine

Set V[k] &V[k] + 1
if V[k<10]
  goto next_line
endif

// Display random lines
Message "&text[msg]"

Return

// Read random line in given text file
include "read_random_record.txt"

 

Remarks
In GuiXT there is no "subroutine" concept which would be appropriate to implement such functions. Here we simulate the subroutine call by putting the name of a label  into a variable RETURNLABEL and then return via

goto "&V[RETURNLABEL]"

If things become more complicated than this example we suggest you use JavaScript or VB.NET calls, since GuiXT scripting is not the appropriate tool for programming complex algorithms.

 


Include "read_random_record.txt"

//-----------------------------------------//
// Read random record from text file       //
//                                         //
// -> FILENAME                             //
// -> RETURNLABEL                          //
//                                         //
// <- LINE                                 //
// <- MAXRECORDS                           //
//-----------------------------------------//

label read_random_record

Set V[LINE] ""
OpenFile "&V[FILENAME]" -noDelimiter
if not Q[ok]
  goto "&V[RETURNLABEL]"
endif

Set V[MAXRECORDS] 1

label random_read_file_1

ReadFile "&V[FILENAME]" "LINE"
if Q[ok]
  Set V[MAXRECORDS] &V[MAXRECORDS] + 1
 
goto random_read_file_1
endif

Set V[MAXRECORDS] &V[MAXRECORDS] - 1
CloseFile "&V[FILENAME]"

if V[MAXRECORDS=0]
  goto "&V[RETURNLABEL]"
endif

// next random number
if not V[RANDOM]
  Set V[RANDOM] "&V[today_hms]" // seed
endif

Set V[RANDOM] &V[RANDOM] * 16807
Set
V[Q] &V[RANDOM] / 2147483647 decimals=0
Set V[Q] &V[Q] * 2147483647
Set V[RANDOM] &V[RANDOM] - &V[Q]
IF V[RANDOM<0]
 
  Set V[RANDOM] 0 - &V[RANDOM]
ENDIF

Set V[M] &V[RANDOM] / &V[MAXRECORDS] decimals=0
Set 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
OpenFile
"&V[FILENAME]" -noDelimiter
if not Q[ok]
  goto "&V[RETURNLABEL]"
endif

Set V[K] 1
label 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
InputAssistant