Reversing elements

Again, to maintain the dynamics of a list, it may be necessary to reverse the elements. To accomplish this, Tcl provides the lreverse command. The syntax is as follows:

	lreverse list

How to do it…

In the following example, we will use the lreverse command to reverse the elements in our list. Return values from the commands are provided for clarity. Enter the following command:


% lreverse {a b c d e}
e d c b a

How it works…

The lreverse command accepts a list and returns a list containing the elements in reverse order.

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.