Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Can I determine if a parameter is an array or not?

    Scheduled Pinned Locked Moved
    Gcode meta commands
    3
    5
    219
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DonStaufferundefined
      DonStauffer
      last edited by DonStauffer

      "#" gives an error on a non-array non-string, so that won't work; I was hoping it would just give zero or one or something. I don't see an "istype" or "isarray" function. Trying to subscript a non-array gives an error, even if wrapped in "exists", so that won't work. So the only thing I see to do is always pass an array if it ever will be an array, and just pass a 1-element array if I don't need more than one item. I'm trying to improve on that. Anything I'm missing?

      A while back I put in an enhancement request to have isnan return true for an array rather than an error. That would allow differentiation of scalar numbers from arrays, which is probably most cases, but wouldn't differentiate arrays from other types, like strings. I thought to modify that request somehow, but I'm not sure if I can change it, or what would be a good way to suggest doing this.

      dc42undefined MJLewundefined 2 Replies Last reply Reply Quote 0
      • dc42undefined
        dc42 administrators @DonStauffer
        last edited by dc42

        @DonStauffer if neither of the following works then I can't think of another way at present:

        exists(#var.x)
        exists(var.x[0])
        

        The second one would of course also return false for an array of zero length.

        Duet WiFi hardware designer and firmware engineer
        Please do not ask me for Duet support via PM or email, use the forum
        http://www.escher3d.com, https://miscsolutions.wordpress.com

        DonStaufferundefined 1 Reply Last reply Reply Quote 0
        • MJLewundefined
          MJLew @DonStauffer
          last edited by

          @DonStauffer The LiveCode development environment in which I mostly work has automatic variable typing, which is VERY convenient, but you still need to test for arrays using something akin to "if the keys() of [variable] is not empty then". That works without a hitch, but I avoid it by being diligent in appending "A" to the name of any array variable.

          DonStaufferundefined 1 Reply Last reply Reply Quote 0
          • DonStaufferundefined
            DonStauffer @MJLew
            last edited by

            @MJLew I'm not worried about mistakes. I want to be able to send a parameter which may or may not be an array, on purpose, and detect which in the called macro.

            1 Reply Last reply Reply Quote 0
            • DonStaufferundefined
              DonStauffer @dc42
              last edited by

              @dc42 Both of these produce errors.

              ;exists(#var.x)
              ;exists(var.x[0])
              
              var x = {1,2}
              var y = 3
              
              ;   Method 1
              
              if exists(#var.x)           ;   expected an identifier
                  echo "exists(#var.x)"
              else
                  echo "! exists(#var.x)"
              
              if exists(#var.y)
                  echo "exists(#var.y)"
              else
                  echo "! exists(#var.y)"
              
              ;   Method 2
              
              if exists(var.x[0])
                  echo "exists(var.x[0])"
              else
                  echo "! exists(var.x[0])"
              
              if exists(var.y[0])     ;   Cannot index into variable or parameter 'y^' of non-array type
                  echo "exists(var.y[0])"
              else
                  echo "! exists(var.y[0])"
              
              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Unless otherwise noted, all forum content is licensed under CC-BY-SA