Page 1 of 1

Wrap operator

Posted: Tue Jul 24, 2018 4:43 pm
by MaxV
Hello,
I never heard of the wrap operator, how it works?
Please can you explain me the math of it?

Re: Wrap operator

Posted: Tue Jul 24, 2018 4:48 pm
by FourthWorld
Allows you to have a lengthy repeat using a shorter counting range:
http://livecode.wikia.com/wiki/Wrap

Re: Wrap operator

Posted: Fri Aug 24, 2018 12:11 pm
by [-hh]
This relates to LC Script AND LC Builder:

The "wrap" operator is made to adjust the mod operator from counting zero-based (as other languages need it) to count one-based (as LC needs it).

Assume you have a list "A,B,C,D" and you wish to use the items in a never ending row.

Using zero-based counting you count (starting from zero) item number 0,1,2,3,0,1,2,3,0,1,...
And you can achieve this (expressed for comparison in LCS/LCB) by repeating "add 1 to counter".
Then
     (1) put counter mod 4 into counter

Using one-based counting you count (starting from one) item number 1,2,3,4,1,2,3,4,1,2,...
And you can achieve this in LCS and LCB by repeating "add 1 to counter".
Then
     (2) put ((counter-1) mod 4) + 1 into counter
or
     (3) if counter > 4 then put 1 into counter (in LCB use "if ... end if")
or
     (4) put counter wrap 4 into counter

The comparison of (1) and (4) shows a simple rule:
For zero-based counting use mod, as in (1),
for one-based counting use wrap, as in (4).

[Similar is valid for counting negative values downwards.]