.comment-link {margin-left:.6em;}

melGeek

Thursday, February 10, 2005

comments, arrays and tokenize

I forgot to mention comments. You can easily provide feedback and notes to the users of your scripts (including yourself) by adding comments. Like this...

//this is a comment
int $firstNum = 1;
int $secondNum = 2;
//lets add the two variables
print ($firstNum + $secondNum + "\n");

OK thats pretty easy, sorry I didn't mention it earilier.

A quick note on arrays. An array is a varible that contains more than one peice of information. Like this...

int $myNumbers[] = {5, 7, 9};

Of course you can affect all or just parts of the array. You'll need to specify which part...

{
int $myNumbers[] = {5, 7, 9};
print ("The first number in the array is " + $myNumbers[0] + "\n");
print ("The second number in the array is " + $myNumbers[1] + "\n");
print ("The third number in the array is " + $myNumbers[2] + "\n");
}

Keep in mind the first item is labeled 0, not 1. I hope that makes sense for you. Now on to "tokenize".

Tokenize is a function that takes a string and breaks it into an array via a character you specify. Lets say you have this...

string $myString = "lf_Arm_JNT";

As in "Left arm joint". You simply want to rename it to rt_Arm_JNT. Here is how tokenize can help.

{
string $myString = "lf_Arm_JNT";
string $right = "rt";
string $newString;

//we'll need another string to store the parts of the first, hang in there I'll show ya
string $myToks[];

tokenize($myString, "_", $myToks);
string $newString = ($right + "_" + $myToks[1] + "_" + $myToks[2] + "\n");
print ($newString + "\n");
}

So tokenize reads like this in english...

tokenize(this string, by this character, and store all the parts in this other string)

pretty cool beavis.

3 Comments:

  • Hey, Chadly -

    Your use of tokenize is enlightening for sure, but to change part of a node name, the substitute command will getcha there pretty fast.

    string $myString = "lf_Arm_JNT";
    $myString = `substitute "lf_" $myString "rt_"`;
    print($myString+"\n");

    Make sure you use the evaluators (``) when you use substitute.

    -j

    By Anonymous John H, at Monday, March 07, 2005 6:16:00 PM  

  • Hey Chadly,

    You may already know this. I found toNativePath and fromNativePath is great in changing back or froward slashes within a string instead of using tokenize.

    example
    This will change \ to /.........
    toNativePath(string)

    -steve

    By Blogger Steve Gould, at Tuesday, May 24, 2005 4:09:00 AM  

  • Oh yeah I heard about those commands, but forgot about them. I appreciate all the comments and tips, keep the mel goodness coming, thats why I started this lil' old blog.

    Cheers.

    By Blogger Chad, at Tuesday, May 24, 2005 10:35:00 AM  

Post a Comment

Links to this post:

Create a Link

<< Home