1

What I want to do is only remove the first item in the list. I checked out the documentation and it seems that lremove is the command that is used to remove items from a list.

Here what I tried:

set list {1 2 3 4 5}
lremove $list 0
puts $list

I would like the above code to print out: 2,3,4,5. But instead it says lremove is invalid command name.

mrcalvin
  • 3,291
  • 12
  • 18
Hannah J.
  • 43
  • 7
  • 1
    You must have been looking at documentation for a different version of Tcl than the one you're using. The `lremove` command was added in Tcl 8.7, which is still in alpha. – Schelte Bron Jun 03 '21 at 15:30
  • 1
    This may help: https://stackoverflow.com/questions/5701947/tcl-remove-an-element-from-a-list – magikarp Jun 03 '21 at 15:31

1 Answers1

2

Tcl doesn't have an lremove command. But it has several alternatives:

set list [lrange $list 1 end]
set list [lreplace $list 0 0]
set list [lassign $list dummy]

And, in 8.7:

set list [lremove $list 0]
lpop list 0

None of these will work with your input data as presented; Tcl uses whitespace to separate list elements, not commas. We need to do some prep and post work too:

set list [split "1,2,3,4,5" ","]
set list [lreplace $list 0 0]
puts [join $list ","]
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 4
    The `lremove` command was added in Tcl 8.7 (via tip 367). Author: you! – Schelte Bron Jun 03 '21 at 15:35
  • Note that `lrange`, `lreplace`, `lassign` and `lremove` take the _list as a value_ and return the new list, and `lpop` takes a _list in a variable_ and modifies it in place. – Donal Fellows Jun 07 '21 at 08:35