Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Tuesday, 21 February 2012

Mel: Query part of a string, given its index and separator: PART 3

Update from PART 2.
Thanks to Viktoras Makauskas for the suggestions and link.
// get an item from a string given the string, separator and index range // ld_getName "L_test_me_out_bind_jnt" "_" 2 -2 ; global proc string ld_getName(string $obj,string $separator,int $start,int $end) { string $buffer[],$name ; int $numTokens=`tokenize $obj $separator $buffer` ; if($start<0) $start=$numTokens+$start ; if($end<0) $end=$numTokens+$end ; if($start<0 || $start>=$numTokens || $end<0 || $end>=$numTokens || $start>$end) return "" ; $name=$buffer[$start] ; for($n=($start+1);$n<=$end;$n++) $name+=$separator+$buffer[$n] ; return $name ; }
Changes include:
  1.  Separating the indexing to individual ints instead of a string that needs to get split.
  2.  Using the indexes to specify the token, not the separator.
  3.  More error checking.
Main difference is 0 -1 will now return the entire string rather than all but the last token (as it does in Python), mainly for functionality due to the removal of the colon. Can still return one item using the same int (ie the last token would be -1 -1).

Monday, 20 February 2012

Mel: Query part of a string, given its index and separator: PART 2

Update to previous post (here), needed to grab a range of items from a string given a separator and index/range (supports reverse and extended indexing).
I do have a horribly sneaky suspicion there is already a built in command for this...
Will work with ranges given as "0", "-1", "0:","-2:", ":2", ":-2", "1:3" and "-3:-1" etc.
// get an item from a string given the string, separator and index number/range as string (supports reverse indexing) //ld_getName "L_test_me_out_bind_jnt" "_" "2:-1" ; global proc string ld_getName(string $obj,string $separator,string $index) { string $mulBuffer[],$indBuffer[],$name ; int $range[],$start,$end ; tokenize $index ":" $mulBuffer ; $range[0]=int($mulBuffer[0]) ; if(startsWith($index,":")) { $mulBuffer[1]=$range[0] ; $range[0]=0 ; } else if(endsWith($index,":")) $mulBuffer[1]=-1 ; tokenize $obj $separator $indBuffer ; $start=$range[0] ; if($range[0]<0) $start=size($indBuffer)-(abs($range[0])) ; if(size($mulBuffer)>1) { $range[1]=int($mulBuffer[1]) ; $end=$range[1] ; if($range[1]<0) $end=size($indBuffer)-abs($range[1]) ; if($end>(size($indBuffer)-1)) return "" ; $name=$indBuffer[$start] ; for($n=($start+1);$n<$end;$n++) $name+=$separator+$indBuffer[$n] ; } else { if($start<0) $name=$indBuffer[size($indBuffer)-int(abs($start))] ; else $name=$indBuffer[$start] ; } return $name ; } Would appreciate any feedback (I know how much simpler this is in python, was curious how simple it was to do in mel).
Python, i think, would handle like this:
"L_test_me_out_bind_jnt".split("_")[2:-1]

Thursday, 16 February 2012

Mel: Query part of a string, given its index and separator

UPDATE: Updated version with better functioning here.
I realised how often I was using a mix of the tokenize, startString and endString to get sections of a string, whether the start or end (various ways) or the middle (ie getting the side "L_*" or "R_*" or type of object "*_ctrl" "*_bind_jnt") etc, and I loved the way python handles this instead (especially the reverse indexing) so I come up with this.
global proc string ld_getName(string $obj,string $separator,int $index) { string $buffer[],$name ; tokenize $obj $separator $buffer ; if($index<0) $name=$buffer[size($buffer)-int(abs($index))] ; else $name=$buffer[$index] ; return $name ; } //ld_getName "L_test_me_out_ctrl" "_" -1 ;
EDIT: Changed positive indexing to match 0 = start, -1 = end

Tuesday, 31 January 2012

Mel/Python: Adding number padding

I was looking through some older scripts and hit across my dirty attempt to add number padding, and I was surprised it actually worked.
I went about tidying it up and came up with this little number in mel.
global proc string rt_addPadding(int $num,int $padding) { int $lengthNum=size(string($num)) ; string $padString; if($lengthNum<$padding) for($i=0;$i<($padding-$lengthNum);$i++) $padString=$padString+"0" ; return $padString+string($num) ; }
Which works ok, and then a couple of minutes searching I came across a post by Nathan which instead uses python (and is far better).
string $pad = `python ("'%0"+$padding+"d' % "+$num)`;
This then is used as this.global proc string rt_addPadding(int $num,int $padding) { return `python("'%0"+$padding+"d' % "+$num)` ; }
Useful :)