zl rot and 'wrapping'

Nov 4, 2008 at 1:15am

zl rot and 'wrapping'

hi,

zl rot does something like this:

1 2 3 4 -> 2 3 4 1
or

a b c d -> c d a b

where items in the list get pushed off the end and get moved to the other side of the list.

i am looking for zl rot alternative that doesn’t wrap the values round, some thing like this:

1 2 3 4 -> 2 3 4 0
or
a b c d -> 0 0 a b

thanks, james.

#40651
Nov 4, 2008 at 3:28am

try making a [js] object with the following code. you can specify the rotation amount as an argument to the js object (i.e. [js rotate.js 2]) or by an int to the right inlet.

inlets=2;
outlets=1;
list.immediate = 1;
msg_int.immediate = 1;

function msg_int(theint)
{
jsarguments[1] = theint;
}

function list()
{
var newarray = [];

if (Math.abs(jsarguments[1]) >= arguments.length)
{
for (i = 0; i < arguments.length; i++)
{
newarray[i] = 0;
}
}
else if (jsarguments[1] > 0)
{

for (i = 0; i < arguments.length; i++)
{
if ((i + jsarguments[1]) > (arguments.length – 1))
{
newarray[i+jsarguments[1]-arguments.length] = 0;
}
else
{
newarray[i+jsarguments[1]] = arguments[i];
}
}
}
else if (jsarguments[1] < 0)
{

for (i = 0; i < arguments.length; i++)
{
if ((i + jsarguments[1]) < 0)
{
newarray[i+jsarguments[1]+arguments.length] = 0;
}
else
{
newarray[i+jsarguments[1]] = arguments[i];
}
}
}
else if (jsarguments[1] == 0)
{
for (i = 0; i < arguments.length; i++)
{
newarray[i] = arguments[i];
}
}
outlet(0, newarray);
}

#143967
Nov 4, 2008 at 3:38am

actually, i think this version works a little better. the version above wouldn’t work if the first element of the list wasn’t a number. try this:

inlets=2;
outlets=1;
anything.immediate = 1;
msg_int.immediate = 1;

function msg_int(theint)
{
jsarguments[1] = theint;
}

function anything()
{
var newarray = [];
arguments = arrayfromargs(messagename, arguments);

if (Math.abs(jsarguments[1]) >= arguments.length)
{
for (i = 0; i < arguments.length; i++)
{
newarray[i] = 0;
}
}
else if (jsarguments[1] > 0)
{

for (i = 0; i < arguments.length; i++)
{
if ((i + jsarguments[1]) > (arguments.length – 1))
{
newarray[i+jsarguments[1]-arguments.length] = 0;
}
else
{
newarray[i+jsarguments[1]] = arguments[i];
}
}
}
else if (jsarguments[1] < 0)
{

for (i = 0; i < arguments.length; i++)
{
if ((i + jsarguments[1]) < 0)
{
newarray[i+jsarguments[1]+arguments.length] = 0;
}
else
{
newarray[i+jsarguments[1]] = arguments[i];
}
}
}
else if (jsarguments[1] == 0)
{
for (i = 0; i < arguments.length; i++)
{
newarray[i] = arguments[i];
}
}
outlet(0, newarray);
}

#143968
Nov 4, 2008 at 3:06pm

yeah, i was hoping not to need javascript….

#143969
Nov 4, 2008 at 8:22pm

Javascript is your friend……..

Any particular reason you don’t want to use js?

I suppose you could figure out some complicated logic with a large combination of standard max objects. Really all you have to do is put the list through [zl rot x] where x is the rotation number, and then figure out a way to set the first x elements in the list to 0 (if x is positive) or set the last x elements in the list to 0 (if x is negative). It would also be good practice to build in some error-proofing by adding some logic to deal with situations like when x is 0, or when the absolute value of x is greater than the length of your list. By now you can see it can get reasonably complicated.

Or…

you can just my script below, which works perfectly.

If you’re looking for a pre-made object that does what you want it do, it likely doesn’t exist because that would kinda be a wierd object that wouldn’t get used very often.

#143970

You must be logged in to reply to this topic.