lreplace performs linsert

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf lreplace performs linsert

  • Creator
    Topic
  • #50369
    Todd Lundstedt
    Participant

      Cloverleaf 5.5Rev1 on AIX 5.3

      What am I missing here…

      Code:

      tcl>lreplace {a b c d e} 1 1 foo
      a foo c d e


      All works just as described in the online tclhelp.

      However…

      Code:

      tcl>lreplace {pv1 {} 0 {} x {} {} {000000^xxxxx doctor} {} {} emm} 7 1 {000000^other doctor}
      pv1 {} 0 {} x {} {} {000000^other doctor} {000000^xxxxx doctor} {} {} emm


      That inserts at index 7 rather than replacing index 7, taking an 11 element list and turning it into a 12 element list.  What tidbit am I missing?

    Viewing 1 reply thread
    • Author
      Replies
      • #65794
        Charlie Bursell
        Participant

          You are mis-reading how to use lreplace.

          lreplace  

          If I had a list and wanted to just replace index 7 (0-based) I would:

          set newList [lreplace $oldList 7 7 $newValue]

          lreplace and linsert both have gotcha’s associated.  If there are not enough items in the list you will get a Tcl error.  For example, in the above if the list had less than 8 items (7, 0-based) you would get a Tcl error.

          If there is any doubt here is what I do.  Assume I am going to lreplace index 7 and I am not sure my list will always have that many

          while {[llength $oldList] <= 8} {lappend oldList {}} This will save you *MANY* headaches  ðŸ™‚

        • #65795
          Todd Lundstedt
          Participant

            Gotcha… I was reading

            lreplace string first last

            as

            lreplace string first length

            Thanks, Charlie!

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