Wrap operator

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
MaxV
Posts: 1579
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: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
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”