Error getting tags :
error 404Error getting tags :
error 404 wrap | revDocs | RunRev
Welcome Guest (Log in)
Product Edition
Version
wrap
Basics
Expander triangle
Type
Operator
Syntax

number wrap divisor

See Also
Introduced
2.9
Environment
Desktop, Web and Server
Platform Support
Mac OS X,Windows,Linux
Security
None required
Summary
Wrap allows the user to ensure the value of a variable stays within a specified range.
Examples

repeat with x = 1 to 10
put item x wrap 3 of "1,2,3" & comma after tOutput
end repeat
-- evaluates to 1,2,3,1,2,3,1,2,3,1
repeat with x=1 to 9
put item x wrap 2 of "1,2" & comma after tOutput
end repeat
--evaluates to 1,2,1,2,1,2,1,2,1

Additional Comments
Expander triangle

The wrap function makes it easy to loop successively over a fixed number of items in a list. When cycling through the items of a list, the divisor parameter specifies which item will cause the cycle to loop back to the beginning of the list. This means that any number outside this range is mapped to a number within it.

For example, if we had 5 wrap 3, the number 5 would be mapped to the number 2 as this is where the iterator would be pointing on the 5th iteration ie. 1, 2, 3, 1, 2 . Therefore 5 wrap 3 is 2.

The mathematical formula implemented by the wrap operator is:

x wraps y = ((x-1) mod abs(y)) +1 if (x >= 0)

= -((x-1) mod abs(y)) +1 if(x < 0)

User Comments
Expander triangle
User thumbnail
michael@michaelsmanias.com
StarStarStarStarStar
Mar 5, 2010
tOutput after each loop in the example evaluates to a 'list' ending in a comma. In the "See Also" I would recommend seeing MOD rather than / .
A problem with this function is that 0 wrap y is 0. This might create a break in the symmetry of your counter and its position in your cycle. For example if you have three items in a list each time you increment your counter up by three you get the same number: 1 wrap 3 = 4 wrap 3 = 7 wrap 3 but if you decrement your counter there is a break around zero 7 wrap 3 = 4 wrap 3 = 1 wrap 3 <> -2 wrap 3 . Wrap does afford a symmetry: x wrap y = -x wrap y
If you want need the relationship: (x+yn) wrap_nb y = (x-yn) wrap_nb y then do


function wrap_nb x,y
if x> 0 then
return ((x-1) mod abs(y)) +1
else
return (x mod y) + y
end if
end wrap_nb