[MEL] undeclared variable error? Please help.


#1

When running the following code, I get the error:

// Error: for ($item in $items)
//
// Error: Line 41.25: “$items” is an undeclared variable. //
// Error: menuItem -label $item -parent $dropdown;
//
// Error: Line 43.31: “$item” is an undeclared variable. //

If I take that for loop out of the procedure and slap a declaration of $items before it, it works fine.
The declarations of $items are in the scope of the procedure, so it should work, right?
Please help me figure this out.

proc string[] populateDropdown(string $location, string $dropdown)
{
    string $modifiedItems[];
    
    if ($location == "shelf")
    {
        global string $gShelfTopLevel;
        string $items[] = `tabLayout -q -childArray $gShelfTopLevel`;
    }
    
    if ($location == "toolbar")
    {
        global string $gMainWindow;
        string $processItems[] = `window -q -menuArray $gMainWindow`;
        
        //Trim menus down to reasonable names and remove hotbox menus.
        string $items[];
        for ($item in $processItems)
        {
            if (startsWith($item, "Hotbox"))
            {}
            else
            {
                if (startsWith($item, "main"))
                {
                    if (endsWith($item, "Menu"))
                    {
                        $item = substituteAllString($item, "main", "");
                        $item = substituteAllString($item, "Menu", "");
                        //append to $modifiedItems - you'll need to return that, so that if one of those is chosen you can revert it.
                        //You'll use stringArrayContains() later on.
                        $items = stringArrayCatenate($modifiedItems, stringToStringArray($item, ""));
                    }
                }
                $items = stringArrayCatenate($items, stringToStringArray($item, ""));
            }
        }
    }
    
    
    for ($item in $items)
    {
        menuItem -label $item -parent $dropdown;
    }
    
    return $modifiedItems;
}

#2

Try to declare it outside of if statement. After

string $modifiedItems[];

#3

Well, I won’t pretend to understand why it wasn’t working, but that fixed it! Thanks!