Example off of Tcl Programming wiki - TCL
This is a discussion on Example off of Tcl Programming wiki - TCL ; Robert Hicks wrote:
>
> lreplace $x 1 bar, ;#--> foo bar, and grill <------- this doesn't
> work in 8.5.1
> (robert) 36 % lreplace $x 1 bar,
> bad index "bar,": must be integer?[+-]integer? or end?[+-]integer?
> (robert) ...
-
Re: Example off of Tcl Programming wiki
Robert Hicks wrote:
>
> lreplace $x 1 bar, ;#--> foo bar, and grill <------- this doesn't
> work in 8.5.1
> (robert) 36 % lreplace $x 1 bar,
> bad index "bar,": must be integer?[+-]integer? or end?[+-]integer?
> (robert) 37 %
ummm, what page. cause that won;t work under any version.
the syntax for lreplace is
lreplace list first last ?element element ...?
from <http://www.tcl.tk/man/tcl854/TclCmd/lreplace.htm>
so your command is interpreted as 2 indexes with no new elements
and "bar," is not a valid index (as the error message explains)
the correct way to get what you want would be
lreplace $x 1 1 bar,
so either you typo'd when getting it from the wiki, or whoever put it
there had a typo, if the 2nd case please correct it (I would but I don't
know the page)
bruce
-
Example off of Tcl Programming wiki
set x {foo bar}
llength $x ;#--> 2
lappend x grill ;#--> foo bar grill
lindex $x 1 ;#--> bar (indexing starts at 0)
lsearch $x grill ;#--> 2 (the position, counting from 0)
linsert $x 2 and ;#--> foo bar and grill
lreplace $x 1 bar, ;#--> foo bar, and grill <------- this doesn't
work in 8.5.1
lsort $x ;#--> bar foo grill
(robert) 30 % set x {foo bar}
foo bar
(robert) 31 % llength $x
2
(robert) 32 % lappend x grill
foo bar grill
(robert) 33 % lindex $x 1
bar
(robert) 34 % lsearch $x grill
2
(robert) 35 % linsert $x 2 and
foo bar and grill
(robert) 36 % lreplace $x 1 bar,
bad index "bar,": must be integer?[+-]integer? or end?[+-]integer?
(robert) 37 %
-
Re: Example off of Tcl Programming wiki
On Feb 22, 6:37 pm, Bruce Hartweg <doNOT...@nowhere.com> wrote:
> Robert Hicks wrote:
>
> > lreplace $x 1 bar, ;#--> foo bar, and grill <------- this doesn't
> > work in 8.5.1
> > (robert) 36 % lreplace $x 1 bar,
> > bad index "bar,": must be integer?[+-]integer? or end?[+-]integer?
> > (robert) 37 %
>
> ummm, what page. cause that won;t work under any version.
>
> the syntax for lreplace is
>
> lreplace list first last ?element element ...?
>
> from <http://www.tcl.tk/man/tcl854/TclCmd/lreplace.htm>
>
> so your command is interpreted as 2 indexes with no new elements
> and "bar," is not a valid index (as the error message explains)
> the correct way to get what you want would be
>
> lreplace $x 1 1 bar,
>
> so either you typo'd when getting it from the wiki, or whoever put it
> there had a typo, if the 2nd case please correct it (I would but I don't
> know the page)
>
> bruce
http://en.wikibooks.org/wiki/Tcl_Pro...g/Introduction
I will correct it...
Robert
-
Re: Example off of Tcl Programming wiki
On Feb 22, 3:37 pm, Bruce Hartweg <doNOT...@nowhere.com> wrote:
> Robert Hicks wrote:
>
> > lreplace $x 1 bar, ;#--> foo bar, and grill <------- this doesn't
> > work in 8.5.1
> > (robert) 36 % lreplace $x 1 bar,
> > bad index "bar,": must be integer?[+-]integer? or end?[+-]integer?
> > (robert) 37 %
>
> ummm, what page. cause that won;t work under any version.
>
> the syntax for lreplace is
>
> lreplace list first last ?element element ...?
>
> from <http://www.tcl.tk/man/tcl854/TclCmd/lreplace.htm>
>
> so your command is interpreted as 2 indexes with no new elements
> and "bar," is not a valid index (as the error message explains)
> the correct way to get what you want would be
>
> lreplace $x 1 1 bar,
>
> so either you typo'd when getting it from the wiki, or whoever put it
> there had a typo, if the 2nd case please correct it (I would but I don't
> know the page)
But remember that you need to capture the output:
set x [lreplace $x 1 1 bar]
or you can use [lset]:
lset x 1 bar
[lset] only works for one list element, and can't delete or change the
number of items in the list.
-
Re: Example off of Tcl Programming wiki
On Feb 22, 3:58 pm, Robert Hicks <sigz...@gmail.com> wrote:
> On Feb 22, 6:37 pm, Bruce Hartweg <doNOT...@nowhere.com> wrote:
>
>
>
> > Robert Hicks wrote:
>
> > > lreplace $x 1 bar, ;#--> foo bar, and grill <------- this doesn't
> > > work in 8.5.1
> > > (robert) 36 % lreplace $x 1 bar,
> > > bad index "bar,": must be integer?[+-]integer? or end?[+-]integer?
> > > (robert) 37 %
>
> > ummm, what page. cause that won;t work under any version.
>
> > the syntax for lreplace is
>
> > lreplace list first last ?element element ...?
>
> > from <http://www.tcl.tk/man/tcl854/TclCmd/lreplace.htm>
>
> > so your command is interpreted as 2 indexes with no new elements
> > and "bar," is not a valid index (as the error message explains)
> > the correct way to get what you want would be
>
> > lreplace $x 1 1 bar,
>
> > so either you typo'd when getting it from the wiki, or whoever put it
> > there had a typo, if the 2nd case please correct it (I would but I don't
> > know the page)
>
> > bruce
>
> http://en.wikibooks.org/wiki/Tcl_Pro...g/Introduction
>
> I will correct it...
Actually the example is correct, just the answers are incorrect. Here
is what the example should look like:
set x {foo bar} ;#--> foo bar
llength $x ;#--> 2
lappend x grill ;#--> foo bar grill
lindex $x 1 ;#--> bar (indexing starts at 0)
lsearch $x grill ;#--> 2 (the position, counting from 0)
set x [linsert $x 2 and] ;#--> foo bar and grill
set x [lreplace $x 1 1 bar,] ;#--> foo bar, and grill
lsort $x ;#--> and bar, foo grill