Wrap operator

Moderators: LCMark, LCfraser

Post Reply
MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Wrap operator

Post by MaxV » Tue Jul 24, 2018 4:43 pm

Hello,
I never heard of the wrap operator, how it works?
Please can you explain me the math of it?
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6174
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Wrap operator

Post by FourthWorld » Tue Jul 24, 2018 4:48 pm

Allows you to have a lengthy repeat using a shorter counting range:
http://livecode.wikia.com/wiki/Wrap
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1785
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Wrap operator

Post by [-hh] » Fri Aug 24, 2018 12:11 pm

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.]
shiftLock happens

Post Reply

Return to “LiveCode Builder”