2 Replies Latest reply on Nov 15, 2016 8:07 AM by ioan_bucsa

    same variable in if/else statements

    evilgoat

      Hello Everyone,

       

      I'm trying to learn the intricacies of the NCM scripting language and trying to answer another question on the forums as some practice (NCM Config Template variable / parsing assistance ) and I'm having some issues at a very basic part of the solution.

       

      I wrote the following piece of the script which is supposed to test if a host has a domain name defined and if yes, then remove it and pass it into @hostname, else just pass the entire NCM.Nodes.SysName to @hostname.

       

      script getHostname(NCM.Nodes @ContextNode )
      {
        string @dot = '.'
        if (@ContextNode.SysName contains @dot)
        {
          int @suffix = indexof(@ContextNode.SysName,@dot) - 1
          string @hostname = substring(@ContextNode.SysName, 1, @suffix)
        }
        else
        {
        string @hostname = @ContextNode.SysName
        }
      }

       

      When I run the script I get the following error:

       

       

      It works perfectly if I use two different variable names, but the point is that I want to give the same variable different values based on an if/else statement.

        • Re: same variable in if/else statements
          jkrenzien

          It is late, but I can't seem to find a way to do this. I was thinking of just breaking it into it's own if statement or doing the else commeand before the if, but all with the same result (well I couldn't find how to do a "does not contain" easily). I will try ot play with it some more at the office tomorrow.

          • Re: same variable in if/else statements
            ioan_bucsa

            Basically you are trying to declare @hostname twice. If you declare it at the beginning of the script and only assign values based on the condition it works.

             

            script getHostname(NCM.Nodes @ContextNode )

            {

              string @dot = '.'

              string @hostname

              if (@ContextNode.SysName contains @dot)

              {

                int @suffix = indexof(@ContextNode.SysName,@dot) - 1

                 @hostname = substring(@ContextNode.SysName, 1, @suffix)

              }

              else

              {

              @hostname = @ContextNode.SysName

              }

            }