PDA

View Full Version : // Error: Illegal operation on data of type string[]. //


tymznd
03-19-2003, 12:47 AM
There has to be an easier way to convert data type[] to strings:

Here is part of my code:
//build cluster name
select $curselected[$i];
string $cluster_name = ($null_name + "_cluster");

// check to see if cluster already exists
$cluster_check = `listRelatives -c $null`;
for ($child in $cluster_check) {
if ($child == $cluster_name)
continue;
}

Here are my questions:
1 - so I am assuming that when you do a --for-- in -- operation on an array[] type that the value for each iteration is still an array?? why would this be? if you are iterating through each element in an array, in what scenario would it make sense that the data type stays of type array[]??

2 - Am I missing some sort of command like "ToString" to convert the data type?

3 - Basically I am trying to create a cluster under a null, but I want to check first to see if the cluster already exists... and if it does, to not proceed with creating the cluster(hence the "continue").... so I also wanted any input if there was a better way to do the pre-check. I was going to go with objExists but I may have the same named objects under different nulls and wasn't sure if that would work

bigfatMELon
03-19-2003, 03:06 AM
type that the value for each iteration is still an array??

No. Your array is a string array. Your named instance of it for that loop is a string. If it were still an array, you'd have to reference a specific index within the loop. As in...

if ($child[0] == $cluster_name);

When you loop over the array with "For-In," each instance of it ($child) is referenced for you of type string. This is what allows you to make a direct comparison between $child and $cluster_name. If they were of different types, you would get an error.

One thing that should help you (and is a good general practice that most MEL authors don't do) is to explicitly declare your variable types at the beginning of your script. Yes, it's more work and more lines of code. Yes, it helps you keep absolutely straight what the type of each variable is. Such as:

string $cluster_check[];
string $child;

$cluster_check = `listRelatives -c $null`;
for ($child in $cluster_check) {
if ($child == $cluster_name)
continue;
}

in the script above it is very clear that $cluster_check is an array while $child is a string. If you attempt to assign either a mismatched datatype, you'll get an error, alerting you to the problem.

-jl

tymznd
03-19-2003, 04:38 AM
Thanks for the reply...

I did try the declarations at the beginning but I still was seeing problems and I think I may be doing something else wrong:

//build cluster name
select $curselected[$i];
string $cluster_name = ($null_name + "_cluster");
$clusterHandle_name = ($cluster_name + "Handle");

// check to see if cluster already exists
$cluster_check = `listRelatives -c $null`;
for ($child in $cluster_check) {
if ($child != $clusterHandle_name) {
cluster -n $cluster_name;

1 - I just realized that I was comparing against the cluster and not the cluster "Handle" which is what I should of been scanning for.

2 - Are there are issues with using the "!=" instead of the "==" with strings? I wouldn't think so.

3 - What happens when there are no children?? cause as this code stands it is not creating any clusters, ever, so the eval is always false... not sure what's happening there. Do you think that if there are no children that it skips the nested statement?

EDIT - OK - yes, because their were no children the cluster command was not being evaluated... so I had to wrap another if:

// check to see if cluster already exists
$cluster_check = `listRelatives -c $null`;
if (size($cluster_check) > 0) {
for ($child in $cluster_check) {
if ($child == $clusterHandle_name) { bla... bla... bla...

mark_wilkins
03-19-2003, 07:36 PM
Using == and != with strings appears to work in 4.5, however it's actually not documented to work that way. If you read the documentation on == and !=, it says that they are only guaranteed to work with integers, floats, and vectors.

The strcmp command is intended to compare the values of strings. strcmp returns different values depending on whether the strings are the same, or if different, which one comes earlier in the alphabet.

-- Mark

CGTalk Moderation
01-14-2006, 04:00 PM
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.