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.

removing string from a list.

Help for those learning Tcl or writing their own scripts.
Post Reply
h
honeybee
Halfop
Posts: 80
Joined: Sun Jan 01, 2006 12:42 pm

removing string from a list.

Post by honeybee »

Code: Select all

proc lremove { list what } {
	while { [set pos [lsearch -exact $list $what]] != -1 } {
		set list [lreplace $list $pos $pos]
	}
	return $list
}
# example:
# set list "aa bb cc"
# set list [lremove $list "bb"] will set list to "aa cc"
its useful when we need to remove a string from a list but sometimes we need to compare two lists and remove items from one list from other if not found.

# example:
# set staticList {aa bb cc dd ee}
# set dynamicList {bb cc dd ee}

Now we need to remove aa from staticList as its not present in dynamicList what are the possibilities.

Sorry for my previous post I didn't make much sense and moved my post from Tcl Faq where proc lremove was posted by Sky to Tcl Help I think it best fits here.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Code: Select all

if {[lsearch -exact $dynamicList "aa"] == -1} {
 lremove $staticList "aa"
}
h
honeybee
Halfop
Posts: 80
Joined: Sun Jan 01, 2006 12:42 pm

Post by honeybee »

Thanks Sir_Fz; help from you are always handy and appreciated
i resolved that issue but this is much simpler than I could think of.
Post Reply