Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:reversearray [2015/09/14] (current)
Line 1: Line 1:
 +====== Reversing Arrays ======
 +====== RhinoScript ======
 +> **Version:​** //Rhino 4.0//
 +> **Summary:​** //​Demonstrates how to reverse an array using [[developer:​rhinoscript|RhinoScript]].//​
 +
 +=====Question=====
 +How can I quicky reverse the order of the elements in an array?
 +
 +=====Answer=====
 +Consider the following subroutine:
 +
 +
 +<code vb>
 + 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
 +
 +</​code>​
 +This can be used as follows:
 +
 +
 +<code vb>
 + 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
 +
 +
 +</​code>​
 +
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/reversearray.txt ยท Last modified: 2015/09/14 (external edit)