Question about arrays….

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Question about arrays….

  • Creator
    Topic
  • #51413
    Tom Rioux
    Participant

      Please excuse me if this questions seems elementary, but I don’t have a lot of experience with arrays.   We have some code that will “set” an array but when we got to “get” the array, it doesn’t seem to bring the elements back in the order in which they were set.   My questions are:

      1.  Is there a reason for the behavior listed above?

      2.  Is there any way to “sort” an array?

      Thanks…

      Tom Rioux

    Viewing 5 reply threads
    • Author
      Replies
      • #70190

        Tom, tcl does not have an array object to my knowledge. Could you post a little of the code so we can see what your code is doing?

        -- Max Drown (Infor)

      • #70191
        Jim Kosloskey
        Participant

          Array get returns the array in no determined order as documented.

          If you want the names sorted try lsort [array names >yourarrayname<].

          If you do that inside a foreach then you can get each array name in order and do something with it like

          foreach name [array names myarrayname] {

               echo “Array: $name Value: $myarrayname($name)”

          }

          email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

        • #70192

          My mistake, there is an array command: http://www.tcl.tk/man/tcl8.4/TclCmd/array.htm

          -- Max Drown (Infor)

        • #70193
          Charlie Bursell
          Participant

            For ergonomic reasons array are store in a hash sequence.  As Jim said use can use the lsort command or, if you know the element names, traverse it like that.

            Arrays are very powerful and useful but require knowlede of their use.  I very seldom write anything that does not have an array somewhere.

          • #70194
            Tom Rioux
            Participant

              Thanks Jim and Charlie,

              Before I posted my question, I had tried the lsort command but it gave me undesired results.   After seeing Jim’s response, I realize I had the syntax wrong.   Thanks for all of your help.   Arrays are pretty cool and my New Year’s resolution is to use array’s and namespaces more often….well that and losing a few pounds…..guess which one I’m more likely to keep?

              Later guys…

              Tom

            • #70195
              Russ Ross
              Participant

                I will also share that I’ve found the parray command usefull when working with arrays.

                The parray command will list out the array index and value stored in that array location in sorted order.

                For grins give this a try:

                hcitcl

                parray auto_index

                Here is a script I downloaded that Charlie posted that uses the parray command to list out the contents of global arrays:

                Code:

                #!/usr/bin/ksh
                # this line to escape the next line from the tcl interpreter
                exec hcitcl “$0” “$@”

                #——————————————————————————–
                # Purpose:
                #
                #    list out the value of all the globals
                #
                # History:
                #
                # 2008.12.29 Russ Ross
                #           – downloaded orignal copy of script posted by Charlie Brusell at URL
                #
                #                 http://clovertech.infor.com/viewtopic.php?t=3256
                #
                #——————————————————————————–

                foreach glob [info globals] {
                  global $glob

                  # Is it an array?
                  if {[array exists $glob]} {
                      echo nThe values of global array $glob are:
                      parray $glob
                      continue
                  }

                  # It is not an array, make sure it exists
                  if {[info exists $glob]} {
                      echo nThe value of global variable $glob is: [set $glob]
                  } else {
                      echo nThe value of global variable $glob is: UNDEFINED
                  }
                }

                Russ Ross
                RussRoss318@gmail.com

            Viewing 5 reply threads
            • The forum ‘Cloverleaf’ is closed to new topics and replies.