developer:scriptsamples:reversearray


Reversing Arrays

RhinoScript

Version: Rhino 4.0
Summary: Demonstrates how to reverse an array using RhinoScript.

Question

How can I quicky reverse the order of the elements in an array?

Answer

Consider the following subroutine:

 Sub ReverseArray(ByRef arr)
 
   Dim i, j, last, half, temp
   last = UBound(arr)
   half = Int(last/2)
 
   For i = 0 To half
     temp = arr(i)
     arr(i) = arr(last-i)
     arr(last-i) = temp
   Next
 
 End Sub

This can be used as follows:

 Sub Main()
 
   Dim arr, i
   arr = Array(1,2,3)
 
   For i = 0 To UBound(arr)
     Rhino.Print arr(i)
   Next
 
   Call ReverseArray(arr)
 
   For i = 0 To UBound(arr)
     Rhino.Print arr(i)
   Next
 
 End Sub
developer/scriptsamples/reversearray.txt · Last modified: 2010/01/26 16:37 (external edit) Driven by DokuWiki Recent changes RSS feed

 © 1997-2012 

McNeel North America Europe Latin AmericaAsia