[TCL/Eggdrop] lreplace : Wat doe ik fout?

Pagina: 1
Acties:

  • RaZ
  • Registratie: November 2000
  • Niet online

RaZ

Funky Cold Medina

Topicstarter
SYNOPSIS
lreplace list first last ?element element ...?
DESCRIPTION
Lreplace returns a new list formed by replacing one or more elements of list with the element arguments. First gives the index in list of the first element to be replaced (0 refers to the first element). If first is less than zero then it refers to the first element of list; the element indicated by first must exist in the list. Last gives the index in list of the last element to be replaced; it must be greater than or equal to first. First or last may be end (or any abbreviation of it) to refer to the last element of the list. The element arguments specify zero or more new arguments to be added to the list in place of those that were deleted. Each element argument will become a separate element of the list. If no element arguments are specified, then the elements between first and last are simply deleted.
Ik heb een var, daar kijk ik of ik een bepaalde string in voor komt:
if {[string match "* #*" $arg]} {
als dit voorkomt, wil ik " #" vervangen door iets anders.
Maar ik snap van de uitleg hierboven dus geen ene moer.
Dit werkt dus niet:
set arg [lreplace $arg " #" " %#"]
set arg [lreplace $arg $arg $arg " #" " %#"]

Hoe werkt dat gekke lreplace? Want ik snap er ff totaal niets van |:(

Ey!! Macarena \o/


Verwijderd

je probeert een soort van string replace te doen, maar daar is deze functie niet voor.
lreplace is een functie waarmee je een list kan aanpassen.
voorbeeld:
Tcl:
1
2
set mylist [list item1 item2 item3 item4 item5]
set newlist [lreplace $mylist 2 3 newitem1 newitem2 newitem3 newitem4]

zal de list {item1 item2 newitem1 newitem2 newitem3 newitem4 item5} als resultaat geven (vanaf het 3e t/m het 4e item vervangen doro nieuwe items).
als je een string replace wil doen zal je toch naar regsub moeten kijken of zelf iets bouwen :p

  • VyperX
  • Registratie: Juni 2001
  • Laatst online: 16-02 16:12
Of je moet een [lsearch] gebruiken om die "#" op te zoeken in je lijst.

code:
1
2
3
4
5
set hashindex [lsearch -exact {#}]
if { $hashindex > -1 } {
  # 2 keer $hashindex omdat we alleen dat element willen replacen
  set arg [lreplace $arg $hashindex $hashindex {%}]
}

Eigenlijk zou je [lsearch -all] moeten doen, en dan met een foreach op alle indices je # vervangen... maar dat kan je zelf wel :)
offtopic:
Is er geen code tag voor TCL?

My Dwarf Fortress ASCII Reward: ~~@~~####,.".D",.B""