Cleaner Scripting Techniques

Hey All,
So, I've been working on a script, and keep running into issues when creating strings, since Maya tends to want to make things into arrays automatically. So, my script is full of these things: [0]
Do you have any techniques for avoiding this hazard which causes lots of "Cannot translate type string[] to string, and other headaches. Any other basic techniques you all use to keep your code cleaner?

QUOTE
So, I've been working on a script, and keep running into issues when creating strings, since Maya tends to want to make things into arrays automatically.

It does not, however most function calls in maya would return string arrays, NOT strings.

So for ovious reasons this would NOT WORK:

string $test=ls;

Simply because ls does not return a string! The error is not about programming style, its about the user being stupid. The computer helps you to enforce you to do not what you think is there, but what IS there in all actuality. Its a clear case of user error. Reason being that a a string array can no more fit into one string container than triangular peg into a round hole.

So i way its telling you your not actually doing whet you think you are doing.

Now if you never declare the type

$myString=ls;

Then your in for a supprise, maya actually KNOWS its a string array and will treat it as such. It will not issue a error. Wich is why its best to tell maya what you THINK it is and Maya will just correct you on execute (so you know something needs to be done)

NOw why cant maya convert it to a string? Well lets assume ls here it returns a string of object names. A string of list could be any of the fllowing and more:

string $array[] =ls;
string $stringAsFirstItem=$array[0];
string $stringAsLastItem=$array[size($array)-1];
string $concatBySpace= stringArrayToString($array, " ");
string $concatByComma= stringArrayToString($array, ", ");
string $concatByNone= stringArrayToString($array, "");

So the computer is telling you that its does not make sense to assume anything about the conversion as its not a self evident one, if it was a nobrainer self evident conversion it would have been done.

But yes if we take your last example code for a example its clear you need to start thinking this programming stuff over again form beginning for a few things.

Actually I'd say that there are some inconsistencies with how maya treats string arrays.. for example if you give the print command a string array it prints it without having to step through each index. So it treats it the array like it is a single string.

And there are a few commands that you'd expect to output an array but instead output a string array for no apparent reason other than perhaps "programmer error". Such a command is the Distance Tool. Which outputs the the name of the resulting tool and it's shape node as a single string! WHY? I have no idea. So in the middle of what I thought was going to be a short script I had to mess around with tokenizing in order to extract the name of the darn tool.

It is way more useful for maya to output everything as an array although honestly I see no reason why it can't also just treat an array like a string when told to do so like the print command does.. if this is not what the user wants then they can treat it like an array instead of string. The whole point of scripting is to make it easy to write simple programs but I won't be holding my breath waiting for Maya to alter the way that this works.

QUOTE(gmask @ 08/17/08, 08:18 PM) [snapback]289858[/snapback]
Actually I'd say that there are some inconsistencies with how maya treats string arrays.. for example if you give the print command a string array it prints it without having to step through each index. So it treats it the array like it is a single string.

Print is a debug command so its expected to know all mayamel types. Allmost all other scripting languages do thsi even tough Normally type counts. Including python.

So while this seems to be incositency in mel it is actually more like a incositency in ALL PROGRAMMING related stuff then. Actually the reason is what i said above.

Example of python:
CODE

array=["foo","bar"]
print array
['foo', 'bar']
array.upper()
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'list' object has no attribute 'upper'
print str(array).upper()
['FOO', 'BAR']

See exact same behaviour.

QUOTE
It is way more useful for maya to output everything as an array although honestly I see no reason why it can't also just treat an array like a string when told to do so like the print command does..

Because its a very common programming error. It would end up doing bug hunting much harder than it actiually is. Same hing happens in python too, while rpint has no problem with arrays pushing a array where string is included does the same. Simly its juta overload question.

But the reason why its not done is that this leads to hard to find programming errors. Trust me those are harder on beginning susers than needing to convert stuff.

Use stringArrayToString!

QUOTE
The whole point of scripting is to make it easy to write simple programs but I won't be holding my breath waiting for Maya to alter the way that this works.

Yes but as i said its better thet this conversion is not done for your ease of use. (thats what stringArrayToString ids there for). See ease of debugging is about 70% ease of use!

Anyway the problem in changing this is that every piece of mel code would need to be reviewed if this change was made. And thats a lot of work. Would hit maya dev bigtime.
QUOTE
Such a command is the Distance Tool. Which outputs the the name of the resulting tool and it's shape node as a single string! WHY?

Thats clearly a tought bug on the developpers part. Id say thst actually trivially easy but the most annoying one is poly info.

Thing is mayas NOT a application like photoshop where theres one team doing stuff. But rather maya has a academic approach. The downside of that is that each module is programmed by different guidelines. The upside is that is easy to do so.

yea mel is just like the english language ... there's an exception for every rule.. sigh

QUOTE(gmask @ 08/18/08, 07:41 AM) [snapback]289872[/snapback]
yea mel is just like the english language ... there's an exception for every rule.. sigh

Good comparasion! It also greatly exemplify how it came to be