How to fix this error?Invalid negative index used to reference array

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

 
Thread Tools Display Modes
  06 June 2018
How to fix this error?Invalid negative index used to reference array

I am writing a script in Mel that will add zero in front of the number part. This little function is success to rename the first one item that i selected. Show up an error// Error: line 20: Invalid negative index used to reference array "$buffer". // . I am just start to learn Mel script, hope someone can help, thanks a lot.


Mel Script:
string $Selection[] = `ls-sl`;
int $Num = size ($Selection);
int $NameLength;
int $NumLength;
string $buffer[];
int $countBuffer[];
string $wordPartA[];
string $AddUnderline;
string $NumPart;
string $Zeronum= "0000000000";
int $countZero= size($Zeronum);
string $Output;


for ($i=0; $i<$Num; $i++)
{
$NameLength = size($Selection[$i]);
$numToken = `tokenize($Selection[$i]) "_" $buffer`;
$countBuffer = {size($buffer)};
int $p = $countBuffer[($i)];
$NumLength = size($buffer[($p-1)]);
$wordPartA[$i] = `substring $Selection[$i] 1 ($NameLength-$NumLength)`;
string $saveZero = `substring $Zeronum 1 ($countZero-$NumLength)` ;
$NumPart = ($saveZero +$buffer[($p-1)]);
$Output = ($wordPartA[$i]+$NumPart);
print ($Output+"\n");

}





 
  06 June 2018

...
string $buffer[];
...

for ($i=0; $i<$Num; $i++)
{
...
  $countBuffer = {size($buffer)};
  int $p = $countBuffer[($i)];
  $NumLength = size($buffer[($p-1)]);
...
}


In your code, the $buffer is empty. The you create a countBuffer with one element which contains the length of buffer what is 0. To the content of p is 0 in your case. And since p is 0, p-1 is -1. But even if the index is not negative, the line $buffer[$p-1] tries to read from an empty array what will give you an invalid result.
__________________
www.renderwiki.com - www.openmaya.net
 
  06 June 2018
Thank you so much. I tried to change the $countBuffer[$i] to [0] , it is success to continue the for loop, first time to write a mel script function even just a sorting name toolXDD I will keep learning on it.
 
  06 June 2018
If you are learning, try python, it is a lot easier in much cases, especially if you are dealing with strings.
__________________
www.renderwiki.com - www.openmaya.net
 
reply share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 08:28 PM.


Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.