PDA

View Full Version : sorting arrays in MEL


berniebernie
03-29-2009, 11:15 AM
Hello cgtalkers


I have an array full of objects (`ls -sl`), which I then process to give me a parallel array full of distances from point A to each object in the array. So far so good.

Now I'm trying to retrieve a list of the objects sorted from closest to furthest. I could use the mel 'sort' command on the array full of distances, but how can I replicate the shifts in array indexes to the one containing the object names ?

I know python handles such data much more efficiently, but right now I can't afford to learn a new language (or can I easily integrate a few python lines - some sort of "eval" into a mel script?).

I'm a little a loss. It's easy enough to create my own bubble/cocktail sort (the arrays are not immensely huge) but I really don't know how to maintain an associative array (containing strings) like php does: $key => $value

Any clues ? Thanks !


And yes, it's time that I move to python; but from what I've heard python in maya is a bastardized version that looks like a hack more than a support language. Perhaps this will change soon ? Integrated PYMEL or something.

greatPumpkin
03-30-2009, 02:06 AM
you can create a new array which combines the distance with the names:

i.e.:

{"1.24=object1","2.3=object2","3.0,"object3"};

then you can sort that array, and then tokenize around the = sign to get the object name

berniebernie
03-30-2009, 09:22 AM
very thoughtful; thank you !

A-OC
03-31-2009, 08:40 PM
Also you should make sure to have enough 0's up front of every "distance", otherwise after sorting you would end up for example with the number 11 being "smaller" than 2, since in the data type string it comes before. So you should check for the longest string and then add a kind of "frame padding" so 11 becomes 0011 and 2 becomes 0002. Then they will sort OK.

CGTalk Moderation
03-31-2009, 08:40 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.