Skip to main content
Solved

Print array

  • February 27, 2022
  • 9 replies
  • 988 views

Forum|alt.badge.img

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

Best answer by David.Nguyen

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

View original

David.Nguyen
Luminary
Forum|alt.badge.img+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

 

 


Forum|alt.badge.img

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:

 


David.Nguyen
Luminary
Forum|alt.badge.img+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


Forum|alt.badge.img

Hi David,

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


Forum|alt.badge.img

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?


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • February 28, 2022

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

 


Forum|alt.badge.img

This time it works perfect as I wish.

Thank you very much, David


David.Nguyen
Luminary
Forum|alt.badge.img+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


Forum|alt.badge.img

Thank you Michael for your great support


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings