PDA

View Full Version : Dividing a number into arrays


efecto
03-07-2009, 06:28 AM
Hi there. I want to divide a number 12345678910 into arrays with 3 numbers in each set.

So in this case 12345678910 will become
$myArray[0] = 910
$myArray[1] = 678
$myArray[2] = 345
$myArray[3] = 12

Does anyonek now of a method of doing this?

Cheers.

Qart
03-07-2009, 07:11 AM
you have tu use math operators / and %

/ (gives you turncated valuea after dividing)
% (gives you reminder after dividing)

example ( 5\2=2 and 5%2=1)

with while loop you have something like this

int $value=12345678910;
float $rem=0;
int $i=0
while ($value>1000)
{
$myArray[$i]=$value%1000;
$i=$i+1;
$value=value/1000;
}
$myArray[$i]=$value;

p.s. maybe somthing wrong with syntax... I didnt test it but if this wont help I can provide working code

eternalsquire
03-09-2009, 02:32 AM
If you like to write Python scripts in Maya, you could do this:

x = '12345678910'

myArray = [ d
for d in
reversed (str ().join ( [ q + (' ' * (not ((r+2)%3)))
for (q,r) in
zip (x, range (len (x)))
] ).split ())
]

I actually believe that Python is a more natural language to design algorithms in than MEL. Give it a try!

The Eternal Squire

DOor
03-09-2009, 05:27 PM
I cleaned up the code Qart and this should work. I am getting some weird results using long Int values. someone tell me im not crazy but when I set "123456789012" and print the variable it returns "-109726571", same if its a float rather than int.

int $myArray[];
int $value = 123456789012;
int $i=0;
while ( $value > 1000 ) {
$myArray[$i] = $value%1000;
$value = `trunc ($value / 1000)`;
$i++;
}

$myArray[$i]=$value;


print $myArray;

tbaypaul
03-10-2009, 03:24 AM
"123456789012"

is a huge integer number in computing...or least it was.....

http://en.wikipedia.org/wiki/Long_integer

eternalsquire
03-10-2009, 03:35 AM
But Python, fortunately, has a huge number representation available.

DOor
03-10-2009, 04:50 AM
yeah, I was hoping MEL had a way of displaying long int variables properly. thanks for the additional info on long integers, I knew of them but not their history. <shrug> oh well.

eternalsquire
03-10-2009, 05:10 AM
Now if you want something *really* powerful, try copying a numpy site-installation from your system installation of Python into Maya's Lib/site-libraries directory.

tbaypaul
03-10-2009, 06:37 PM
yeah, eternalS is right......python is a modern scripting language. With Python, you can move 10,000 element arrays between commands in a few heartbeats....e.g.

import maya.cmds
import random

#a polyplane to work on
maya.cmds.polyPlane(w=24,h=24,sx=99,sy=99,ax=(0,1,0))
#and randomize the pnty attributes for 10K verts
maya.cmds.setAttr ("pPlaneShape1.pnts[0:9999].pnty", *[random.random()*0.1 for x in range(10000)])

* example from a recent podcast at the Autodesk subscription center.

CGTalk Moderation
03-10-2009, 06:37 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.