This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Lappend removes "\"

Old posts that have not been replied to for several years.
Locked
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Lappend removes "\"

Post by Sir_Fz »

When executing "lappend list \[something\]" it will be added to list as [something] instead of \[something\] as I want it.

Can anyone code a "string map" or "regexp" which would convert \[something\] to \\\[something\\\] ? so when "lappend list \\\[something\\\]" is executed, \[something\] will be added to list.

I Hope I was clear enough, and note that \[something\] would be in a var (ofourse, that's why I need the "string map" or "regexp" :P )

Thank You :)
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

The problem is, is that all you are doing is escaping the []'s, which tells tcl that you actually mean []'s and not command substitution.

Use:

lappend variable {\[test\]}

As the {}'s tell TCL you won't be using command substitution.

(Note: Similarly, if setting a variable, set it as such: set variable {\[test\]})
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Thank you strikelight :D I had a feeling that you are the one who's going to reply on this :P I don't know why (although we haven't seen you around lately)

if variable contains for example \[this\] then using: lappend list ${variable} will insert \[this\] into the list :)

Thanx again.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

I've just realized that it doesn't matter whether its $var or ${var} sorry for the mistake, my problem seems to be something else (I was testing it directly on "lappend list \[this\]" instead of $var).

I have a code to remove nicks from a file, but nicks with \ don't seem to be deleted, Here's the code:

Code: Select all

proc spn:rem {hand idx arg} {
  global spnfile spf
  if {$arg == ""} { return 0 }
  set spnick "[string map {\\ \\\\ \[ \\\[ \] \\\]} $arg]"
## exists:spnick checks if $spnick exists in file and set spf to 1 if found.
  exists:spnick $spnick
  if {$spf == "1"} {
    set list ""
    set file [open $spnfile r]
    while {![eof $file]} {
      set nick [gets $file]
      if {![string equal -nocase $nick $spnick]} { lappend list $nick }
    }
    close $file
    set ofile [open $spnfile w]
    foreach snick $list {
      if {$snick != ""} { puts $ofile $snick }
    }
    close $ofile
    putlog "$spnick was deleted."
  } else {
    putlog "$spnick doesn't exist."
  }
 }
So what seems to be the problem ?
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

Most likely the culprit is your unneccessary string map...
Try setting spnick directly to arg and see how that goes.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Thank you, I removed the string map and added it while checking (since in the file they have the "\"; i.e. \[bla\]) and it works perfectly now.
Locked