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
Posts: 1576
Joined: Tue May 28, 2013 2:20 pm
Location: Italy

Wrap operator

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

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

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

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:
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/

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".
     (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".
     (2) put ((counter-1) mod 4) + 1 into counter
     (3) if counter > 4 then put 1 into counter (in LCB use "if ... end if")
     (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”