problem with array

Clovertech Forums Cloverleaf problem with array

  • Creator
    Topic
  • #118459
    Gene Millard
    Participant

      I am setting an array in my tcl.

      set FT($index1) $segment

      index1 equals the below

      2021020186900
      2021020186901

      The index ending in 900 goes into the array first

      When I read the array using the code below I get the information from 2021020186901 first which makes the segments out of  sequence.

      set srch [array startsearch FT]
      while {[array anymore FT $srch]} {
      set key [array nextelement FT $srch]
      set segment2 $FT($key)
      lappend line3 $segment2
      }

      How do I get the information out in the order I put it in?

      Gene Millard

      The Guthrie Clinic
      Sayre, PA

    Viewing 2 reply threads
    • Author
      Replies
      • #118460
        Jeff Dinsmore
        Participant

          It would seem that “array nextelement” doesn’t necessarily return keys in the order they were written.

          The same is true for “array names”, so that’s not surprising.

          If your array keys are consistent date/time stamps, you can just sort them with lsort.

          foreach key [lsort [array names FT]] {

          <do stuff with $FT($key) here>

          }

          Jeff Dinsmore
          Chesapeake Regional Healthcare

        • #118461
          Gene Millard
          Participant

            Thank you that worked.

            Gene Millard

            The Guthrie Clinic

            The Guthrie Clinic
            Sayre, PA

          • #118464
            Charlie Bursell
            Participant

              FYI

              Array keys are not ordered. It isn’t straight-forward to get values out of an array in the same order that they were set. One common alternative is to get the names and then order them.     Internally, Tcl uses a hash table to implement an array.

              You could always use the dict command if there is no logical way to order an array.

          Viewing 2 reply threads
          • You must be logged in to reply to this topic.