PDA

View Full Version : Getting index No. of an array


Buexe
05-27-2003, 11:43 AM
Yo, I was just wondering if there is a mel command that will return the index number of an item in an array as an int, for example:
you have an array $test[] and if you give the command $test[26] it will return 26 as an int

Right now I`m tokenizing to get the int but Im just curious if there is an easier way.
TIA
Buexe

mark_wilkins
05-27-2003, 02:37 PM
Umm.... hmm...

what, you mean something that you can give the string "$test[26]" to pull the 26 out?

Tokenizing is one way. Still, I'm kind of at a loss about why you'd ever, ever, ever need to do this.

-- Mark

Hugh
05-27-2003, 02:51 PM
The other thing that I think he might mean is if he has whatever is stored in $test[26], to find the index of that....

For example:


string $test[];
$test[0] = "val1";
$test[1] = "val2";
$test[2] = "something";

$searchString = "val2";

int $i;

for($i=0; $i < size($test); $i++)
{
if($test[$i] == $searchString)
break;
}

In this code, and the end, $i will equal 1 (the index where "val2" was stored)

mark_wilkins
05-27-2003, 02:56 PM
if that were what he meant, he probably wouldn't be using tokenize to find it.

-- Mark

Hugh
05-27-2003, 03:14 PM
Ah yes....

Hugh's lack of observation strikes again...

didn't read that part of the post....

Just ignore me when I say something like that next time... ;)

Buexe
05-27-2003, 04:08 PM
I would like to extract the index no. to create another array item for a command
For example:
I have an array $test[]
$test[26] has the value "4"
now i need 26 because I want a command that is something like: $result= `doSomething -q -flag pCube1.vtx[26]`;
`doSomething -e -flag $result pCube1.vtx[4]`

That means the array no. is actually a vertex no and the value assigned is another vertex no.. Since it will always be assigned to the vertiex no. of a polyObject Maybe I could call the array $vtx[] and than I don`t need to rename/tokenize it but just get rid of the $-sign in the beginning.
Sound reasonable?
b

Hugh
05-27-2003, 04:11 PM
still not entirely sure what you're after here....

How do you decide which element of $test[] you want to work with?

Buexe
05-27-2003, 04:37 PM
There are basically two actions i would like to do :
1. Do something to all elements of the $test[] array
2. Get user input in the form of a selection of vertices (Ls -sl -fl), when I have extracted the object and vertex number I can ask my $test[] array for the appropriate value (vertex number 26 will go to $test[26] and ask for its value) to create the whole command.

Hugh
05-27-2003, 04:42 PM
Ah - I think I understand what you mean now......

If you select a vertex on a poly object, and do `ls -sl`, you get the string "pCube1.vtx[3]" back... and you want to get the '3' from that....

My initial feeling is to tokenize it, as you said you're already doing, but I'll take a deeper look into this and see if there's an easier way...

Edit: I can't find anything specifically on this....

Do be aware that, when selecting multiple vertices, if you've got two or more with consecutive vertex numbers, you'll get "pCube.vtx[2:4]" (which is vertices 2, 3 and 4)

sedric
05-27-2003, 05:35 PM
im not sure....but would this work??

int $a = 0;
int $thing;

while ($a < size(the array))
{
if (you test to see if it is the one you want) $thing = $a;
$a++;
}


just a thought......its early...so I could be miles off......

Sedric

Hugh
05-27-2003, 05:40 PM
That would work in the case where he wants to go through all of the vertices (although a for loop would be prettier) - it's the one where he just wants to work with the selected vertices that I'm not sure about...

ktpr
05-27-2003, 07:48 PM
heheh I think I'm going to have to do somethign like this. It's sounding to me like you want a 1-1 correspondence between teh verts of one poly and the verts of the other. your problem as far as I can understand it, is converting the users selection to a index that can be used to reference your master poly vert array, from which you will get the corresponding vert from your child array.

To get the users selection as an index, what i think you can do, given the following:

[edited - took out extra command]

given (get verts from selection)

string $verts[] = `filterExpand -sm 31`;

and then use match (regxp) to grab the rear [ ... ] for each element in the string[].

I haven't used regular expressions in a while so please forgive the following if its wrong:

int $vertIndex = `int( match( "\]$[0-9]+[. " , $verts[$num]) ) `

(from the back, starting at ], grab all the numbers and stop at first occurence of [)

could ("[0-9]$+") work too? I haven't tested these expressions out.

hope that helps
cheers
ktpr

Buexe
05-27-2003, 09:54 PM
Yo, KTPR, this seems to be something handy to get the int out of this mess, BUT this match-syntax is powerful but yet so confusing. Your example seems to be very close but yet does produce a syntax error, but I bet it is something very minor. I have not worked with match yet, but I guess it is time to do so now. There is something about match in Mark`s book and seems to be mostly used for validating input. For now I use something I found on Bryan Ewert`s Homepage:

string $node = "pCube1.f[2]";
string $component = `match "\\..*" $node`;
// Result: ".f[2]" //

PS: Has anyone a clue why KTPRs example may not be correct?

PPS: Mama, my brain hurts...

mark_wilkins
05-27-2003, 10:05 PM
Just so you know, there's a WHOLE LOT you can do with regular expressions and match.

I just covered it in the context of validating input because it's good for that and I felt that was a good habit to suggest to people new to scripting.

Anyone who's done a lot of work with Perl knows that Maya's regular expression implementation is seriously broken, which is why I didn't suggest using it in this context. I'd probably try it myself, though.

-- Mark

ktpr
05-28-2003, 01:03 AM
string $node = "pCube1.f[22]";
int $vertNum = match( "[0-9]+\]$", $node);
print($vertNum)

not sure how to get rid of the warning. Btw I this only gets the first whole number from the rear. So if you had something like [4:454] it would return 454.

[edit - I think you could use "[0-9]+:." to get the front part, if you expected a x:y formated vert; also you can use command filterExpand to expand string[] like this.]

there's defintely something off about this match command. I only used regexp in Python and just started looking at mel last week.

btw I just found a lookup table in the scripts/others. It's called lookupTable.mel. I don't think I need a hash table anymore.

cheers
ktpr

mark_wilkins
05-28-2003, 01:33 AM
One thing that's "off" about regular expressions in Maya is that, though clearly documented, the ? quantifier doesn't work. You must use {0,1} instead.

At least, that was true as of 4.5.

-- Mark

wrend
05-28-2003, 06:07 AM
yeah the regExps are soft, floppy, flacid pieces of poo in maya. i wasted a good afternoon trying to do just this, resigning to extracting it in two steps.

Buexe
05-28-2003, 08:39 AM
Hey Mark, you mention in the beginning of the chapter that regular expressions are beyond the scope of your book, so I fully understand that you can not give a detailed documentation of this (more or less good implemented) Mel feature. Your book and all of you who shared your valuable knowledge helped me a lot, so this is what Ill use for now:

select -r pCube1.vtx[2] ;
string $vtx[]=`ls -sl -fl`;
string $no1 = `match "\\[[0-9]+\\]" $vtx[0]`;
string $no2 = `match "[0-9]+" $no1`;
// Result: 2 //

It does the job and if I make a procedure out it I don`t have to retype the match syntax too often.
Thanks again

buexe
:wavey:

mark_wilkins
05-28-2003, 08:40 AM
Looks good to me... :D

-- Mark

CGTalk Moderation
01-15-2006, 07:00 AM
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.