PDA

View Full Version : Sorting an array with both numbers and text?


scottiedoo
05-05-2006, 03:57 PM
Thanks guys for helping me out. I hope this can be done.

I have list of joints in an array and I run a for loop on each joint in the array. Inside this loop I have it do a couple things specifically for the current joint it is on.

What I am looking to do is somehow gather all the information that is gives me in this loop and tie it together so that information can be linked to that joint. Meaning, one thing that this loop does is give me the joints translateX value, and two other values. I need to store those 3 values to that joint name to be use.

Now that isn't the hard part, I know how to do that already. What I want is to be able to do is after the looping is finished and I have all the information i need on those joints, I need to be able to sort the whole list while retaining all the information to their respective joints by a specific order.

You can think about it in database terms that you have a table with information and you can sort the information any way you want. However it is trickier in maya I know.

I know there is a sort command, however I am trying to figure out how I can sort by say the translateX value while still retaining to what joint that value goes to.

So there are two things I need to figure out and you guys to help me. One is to figure out how I should organize this data (the joint name, translateX, value 1, value 2) inside the loop and combining that data into one large group with the other joint data, AND how to sort all that data while still keeping everything in order. I would like to sort it by the translateX (ascending or desending, it doesn't matter)

Thanks for you help and I hope I made myself clear enough. Ask any questions if you don't understand.

-Scott

Stucky
05-05-2006, 04:17 PM
If I understood it right, you can create 3 arrays. One for translate, one for value1 and one for value2. But I don't understand one thing. Do you want to add these 3 values of, say, joint 1, to be in another separate array? what do you mean by "sort the data"?



-S

scottiedoo
05-05-2006, 04:35 PM
Hey Stucky,


Basically I have a joint list and run a loop for each joint in the list. Inside the loop, I get information that is joint name dependent. So I need to keep all that information linked together somehow like this:

[Joint1Name,translateX,value1,value2]
[Joint2Name,translateX,value1,value2]
etc....

However what I want to do after the loop is take all those joints with their information from the loop and sort it by the tranlateX value. so that I would have a new list or order of joints and their information going from we'll say the lowest translateX value to the highest translateX value.

for example (before sorting)

[joint1, 2, 100, 0 ]
[joint2, -1, 50, 50 ]
[joint3, 5, 25, 25 ]

I would want to sort this somehow into:

[joint2, -1, 50, 50 ]
[joint1, 2, 100, 0 ]
[joint3, 5, 25, 25 ]

I don't know if putting all the joints and information along with them would be better? Like

[joint1Name, translateX, value1, value2, joint2Name, translateX, value1, value2, etc...]

andn then sort it? or what..

So i just need a order, it could even be a new joint list but they have to be sorted by the translateX, so however you could get the lowest joint translateX and give me the order to the highest translateX joint.

does that help?

Stucky
05-05-2006, 04:41 PM
I think your numenclature works right. You can do this: imagine this:


string $name = "joint1";
float $value1 = "2";
float $value2 = "6";

you can then create this:


string $arrayElement1 = ($name +", " + $value1 + ", " + $value2);

it creates the first array of the element. Then if you want to separate this, you can use the tokenize.

I am talking only about one joint. To have more joints, you should only run a loop for all the joints a create something like what I said. I'm just giving an ideia. I don't know if you might run into problems when actualy doing this...


EDIT: I forgot to say that you have to actually add that $arrayElement1 to a new array, and then yes, that array will have that data you want...

Hope it helped


-S

scottiedoo
05-05-2006, 09:23 PM
right, that is how i thought i would put it all together and just separate it later,

but now can you know how I can do the sorting thing I was mostly interested in. to sort all the joints by translation X and then giving me the right order the joints should go in?

Stucky
05-05-2006, 09:58 PM
damn. I'm feelling a little stupid because I didn't quite understand the kind of sort you want. Do you want to be able to do what? to print the information? sorry for this scott, but if you could just give a more concret example of what you want, it would be great..perhaps I would understand it better...sorry for this dude....






-S

alredha
05-05-2006, 10:02 PM
hi,

it would be easier if array of arrays exists in mel, i think if you could hold the initial rank of the values and then compaire them with the sorted one, you could have the right indexes
for exemple let try this out with just the names of joints and the translateX values :


string $joints[] = {"joint1","joint2","joint3","join4"}

int $Tx[] = {29,26,32,24}

int $initTx[] = {29,26,32,24} // the same

for ($i=0;$i<size($Tx);$i++){
print ($joints[$i]+" : "+$Tx[$i]+"\n");
}

$Tx = `sort $Tx`;


for ($i=0;$i<size($Tx);$i++)
{
for ($j=0;$j<size($Tx);$j++)
{
if($initTx[$j]==$Tx[$i])
{
print ($joints[$j]+" is "+$Tx[$i]+"\n");
}
}
}

Stucky
05-05-2006, 10:11 PM
thanks for jumping in redha. With that I understand what scott might want. And that was what I was thinking about too. Good work...




P.S.: just to say that you forgot to add ; at the end of the first 3 variable declaration.



-S

scottiedoo
05-06-2006, 12:29 AM
thanks redha for that snippet of code. I think that will work well. But I came across a thought.. What happens if there are two joints that have the same initTx values.

Such as if two joints are one on top of another, then wouldn't it just grab the first matching joint twice?

Is there a way to use that same method you have and removing joints and their initTx out of the array once they are matched with the sorted array? That way we wont' run into a problem with two joints having the same Tx value.


But thats a great help so far and I'm going to see what I can come up with but if you have any ideas, please let me know.

Thanks for your help both of you so far.

Stucky
05-06-2006, 12:34 AM
I think there is a way to remove an element from an array. But it is not a direct way. David Gould explains how to do this in the beggining of his book, Complete Maya Programming (Volume I): An Extensive Guide to MEL and the C++ API. I have it, but I will only be able to see that on next Monday. So if you want to wait I can give you his example. Or if somebody could help meanwhile.......




-S

scottiedoo
05-06-2006, 12:41 AM
actually i found an easier solution, thought probably not that perfect, it will work fine for now.

all i did was inside the if statement to compare the initTx to the Tx arrays... once it found a match I just replaced the initTx[$j] with a really high or strange number that would be really hard to find a match that way when it runs it agian, it will just skip over it and go to the next. So that may work for now.

Stucky... if you find a better way yeah I would love to hear it. Thanks for looking into it for me. I wish maya had 2 dimensional arrays, it would make life a lot easier managing all of this.

Appreciate the help!

Stucky
05-06-2006, 12:45 AM
hehe...ok dude. I will give you the example of David Gould, as soon as I get near my book(monday). If I don't reply till monday, probably its because I forgot to do it(hehe). You can PM me at any time to ask for it...


-S

alredha
05-06-2006, 09:31 PM
here it is a little bit built up of the previous code, i just made a procedure that could convert an int array to string array, eliminating redundancy in the array then convert them back to the string array, sort and return them ready for use :), you can make the same thing for a STRING sorting.........


proc int[] doo_sortThem(int $var[])
{
string $stringTx[];
string $noDuplicates[];
int $sorted[];
int $i = 0;

for ($each in $var){
$stringTx[$i] = $each;
$i++;
}

$noDuplicates = stringArrayRemoveDuplicates($stringTx);

$i = 0;

for ($elem in $noDuplicates){
print ($elem+" ");
$sorted[$i] = $elem;
$i++;
}

return sort($sorted);
}

//*******************

string $joints[] = {"joint1","joint2","joint3","joint4"};

int $initTx[] = {6,10,32,6};

for ($i=0;$i<size($initTx);$i++){
print ($joints[$i]+" : "+$initTx[$i]+"\n");
}

int $sortedTx[] = `doo_sortThem($initTx)`;

for ($i=0;$i<size($sortedTx);$i++)
{
for ($j=0;$j<size($initTx);$j++)
{
if($sortedTx[$i]==$initTx[$j])
{
print ($joints[$j]+" is "+$sortedTx[$i]+"\n");
}
}
}

scottiedoo
05-06-2006, 11:18 PM
redha, I think I love you... haha.. i'm just kidding... but that works great, much better than my little work around and cleaner too..

I really appreciate both of your help very much... Good stuff..

Thanks again!

CGTalk Moderation
05-06-2006, 11:18 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.