Solved

Reding data from .dat-file into ZPL macro

  • 1 February 2022
  • 2 replies
  • 452 views

Badge

Hi,

I would like to read several numeric data as well as text data from a .dat-file into a zpl macro to use some of them for further use.

For example, the .dat file has 5 rows and 3 lines, divided by Tabs, e.g.:

Pos1    3.7   4.2   6.2   8.7

Pos2    9.0   7.5   2.8   5.6

Pos3    0.5   1.8   4.6   4.3

Is it possible to use the READ command for reading the complete .dat-file and to select afterwards some special data for further optimization?

Or would it be possible to read only some special data from the .dat-file for further use, e.g. (Row2, Line 2 = 9.0) and (Row3, Line 3 =1.8)?

 

Best regards

Ralf

 

 

icon

Best answer by Jeff.Wilde 2 February 2022, 20:15

View original

2 replies

Userlevel 7
Badge +3

It’s easier to read just numeric data, but reading mixed string and numeric values is certainly very doable.  Here’s an example using your data set.

! read data from file

numrows = 3
numcols = 5
DECLARE DARRAY, DOUBLE, 2, numrows, numcols
! read numeric data (note: strings are set to zero)
OPEN "test.dat"
FOR i, 1, numrows, 1
FOR j, 1, numcols, 1
READNEXT x
DARRAY(i,j) = x
NEXT j
NEXT i
CLOSE

! print results
FOR i, 1, numrows, 1
FOR j, 1, numcols, 1
PRINT DARRAY(i,j)
NEXT j
NEXT i

! read strings from first part of each line
OPEN "test.dat"
READSTRING ROW1$
P1$ = $GETSTRING(ROW1$, 1)
READSTRING ROW2$
P2$ = $GETSTRING(ROW2$, 1)
READSTRING ROW3$
P3$ = $GETSTRING(ROW3$, 1)
CLOSE

PRINT P1$, " ", DARRAY(1,2), " ", DARRAY(1,3), " ", DARRAY(1,4), " ", DARRAY(1,5)
PRINT P2$, " ", DARRAY(2,2), " ", DARRAY(2,3), " ", DARRAY(2,4), " ", DARRAY(2,5)
PRINT P3$, " ", DARRAY(3,2), " ", DARRAY(3,3), " ", DARRAY(3,4), " ", DARRAY(3,5)

Here’s the output:

 

The ZPL keywords:  READ, READNEXT, READSKIP, & READSTRING offer various methods for getting data from files into the ZPL shell.

Badge

Hi Jeff,

thanks a lot for your detailed description. That was very helpfull to me.

Reply