Solved

Print array

  • 27 February 2022
  • 9 replies
  • 614 views

Badge

Hello OS-community,

how to write a ZPL-code printing elements of a single or multi-dimensioned array  (ie. Array(i), Arrax(i,j)) in one line in the output of the text viewer?

Thanks

icon

Best answer by David.Nguyen 28 February 2022, 15:14

View original

9 replies

Userlevel 7
Badge +2

Hi Lieu-Kim,

 

There is an example in the Help File under: The Programming Tab > About the ZPL > Array Variables 

I’m copying this example here for your reference:

 

DECLARE Z, DOUBLE, 2, 5, 5

FOR i, 1, 5, 1

FOR j, 1, 5, 1

Z(i, j) = i + j

NEXT j

NEXT i

 

FORMAT 8.0 k = 0

FOR i, 1, 5, 1

FOR j, 1, 5, 1

PRINT k, i, j, Z(i,j)

k = k + 1

NEXT j

NEXT i

 

RELEASE Z

 

The second half shows you how you can print the array.

Let me know if this helps, and take care,

 

David

 

 

Badge

Hi David,

 

many thanks for your suggestion, which I have already tried. Unfortunately this is not what I want to achieve.

The out put shows array components in a column but not in a row.

 

The out put should look like below:

 

Userlevel 7
Badge +2

Hi Lieu-Kim,

 

I would do the following:

DECLARE Z, DOUBLE, 2, 5, 5

FOR i, 1, 5, 1

    FOR j, 1, 5, 1

        Z(i, j) = i + j

    NEXT j

NEXT i

result$ = ""

FORMAT 1.0

FOR i, 1, 5, 1

    FOR j, 1, 5, 1

        result$ = result$ + " " + $STR(Z(i,j))

    NEXT j

NEXT i

PRINT result$

RELEASE Z

Which is basically, save an empty string, and add the number to be displayed to that string using the $STR() function. At the end, print the string.

Does that make sense?

Take care,

 

David

Badge

Hi David,

this is exactly what I need. 
Thank you so much for your great help!

Badge

Hi David,

there is a problem with the size limit of string:

Syntax error: String exceeded maximum allowed length of 360 characters in expression.

What is to do?

Userlevel 6
Badge +2

Hi Lieu-Kim,

The maximum length of a STRING array is around 360 characters (it’s “around” 360 because the ZPL pre-processor manipulates a string and sometimes removes spaces or replaces a non-alphanumeric character with a unicode equivalent).

For printing an 2D array in a grid, you should simply have 2 FOR loops; the inner FOR loop will print the each column in the row and the outer FOR loop will move to the next line.  For the inner FOR loop, you simply need to end the PRINT command with a comma:

FOR i = 1, 10, 1

   FOR j = 1, 10, 1

      PRINT $STR(Z(i, j), $TAB(),

   NEXT 

   PRINT

NEXT

 

Badge

This time it works perfect as I wish.

Thank you very much, David

Userlevel 7
Badge +2

Well, you can thank @MichaelH for that. I wasn’t aware of this limitation. I’ve learned something today.

Though, if you can avoid having a print in a loop, it might be preferable.

Take care,

 

David

Badge

Thank you Michael for your great support

Reply