Question about arrays….

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

  • Creator
  • #51413
    Tom Rioux

    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?


    Tom Rioux

Viewing 5 reply threads
  • Author
    • #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

      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)”



    • #70192

      My mistake, there is an array command:

      -- Max Drown (Infor)

    • #70193
      Charlie Bursell

      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

      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…


    • #70195
      Russ Ross

      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:


      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:


      # 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

      foreach glob [info globals] {
        global $glob

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

        # 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

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

Forum Statistics

Registered Users
Topic Tags