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.

array unset procedure

Old posts that have not been replied to for several years.
Locked
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

array unset procedure

Post by cerberus_gr »

Hello,

I use tcl 8.0 which doesn't support "array unset" command.
How could I create a proc to do the same as unset option?
User avatar
CrazyCat
Revered One
Posts: 1359
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

set $array_name ""
unset $array_name

Not tested, but I think it may works
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

array unset arrayName ?pattern?

I just want to unset an element and not the whole array...

hmmmm

I think about that again and it seems that I just want to delete an element from a LIST and not from an array where I could do this you said CrazyCat.

Code: Select all

proc lremove { list element } {
#  uplevel????
   for {set i 0} {$i < [llength $list]} {incr i} {
       if {[string match $element [lindex $list $i]]} { lreplace $list $i $i }
   } 
}
I have to use uplevel. right?
Any help about it?
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

use unset arrayname(stored-value) same this as array unset arrayname stored-value
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

I have a list in an array and I want to remove an element from the array and not from the list... I could do this in the way I said before, but I want to make a procedure for this.

The only way that I have thougthis to call lremove like:
set listname [lremove listname guss]

and lremove procedure to return a new list without these elements...

Any ideas to make a procedure which returns 0 or 1 and it sipmly removes the list element from the array element?

Example:
Tcl: invite {loutrino_arkoudaki thanoulis thanoulis2 thanoulis2323 skynet mar|a guss pranky white_tiger}

array element: invite
array get $array invite returns the list
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

call globaly a variable, and use set variable [lreplace.....]
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

something like this should do it..

Code: Select all

array unset yourarray $element
Don't forget to call globaly the array via an global yourarray and no need of $ in front of yourarray like $yourarray..
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

I've said this before...

Post by user »

caesar wrote:something like this should do it..

Code: Select all

array unset yourarray $element
Don't forget to call globaly the array via an global yourarray and no need of $ in front of yourarray like $yourarray..
And don't forget that the part you called 'element' is actually a PATTERN (glob), so to unset single elements, using the good old 'unset' would probably make your life alot easier.
Have you ever read "The Manual"?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

he said and I quote "I just want to unset an element and not the whole array... " so.. that should do what he asked..
Once the game is over, the king and the pawn go back in the same box.
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

I have the opinion that tcl8.0, which I'm using, does not support array unset command and that's why I have all these problems :P

Sir_Fz I have thougth of this, but I don't know how I should give as argument.

Code: Select all

proc lremove { list element } {
    set temp ""
    for {set i 0} {$i < [llength $list]} {incr i} {
        if {![string match $element [lindex $list $i]]} { lappend temp [lindex $list $i] }
    } 
    return "$temp"
}
Would be correct this one?

Code: Select all

proc lremove { array_element list_element } { 
    global $array_element
    for {set i 0} {$i < [llength $array_element]} {incr i} {
        if {![string match $list_element [lindex $array_element $i]]} { lreplace $array_element $i $i }
    } 
}
Example:
I have this array:

element1 data1 element2 {data2.1 data2.2 data2.3} element3 data3

and I simply to use:
lremove <array_element>(proc 1) or <listname> (proc 2) element (glob style)

and not:
set array_element [lremove $array_element list_element]

Thx for your time :)
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:he said and I quote "I just want to unset an element and not the whole array... " so.. that should do what he asked..
Using array unset to unset single array elements does not work without escaping some chars in the pattern first. (you could argue it works most of the time, but that's not good enough)
cerberus_gr wrote:lremove <array_element>(proc 1) or <listname> (proc 2) element (glob style)
You seem to be a bit confused about arrays/lists

A list is just a way to format your data while an array element is a place to store the data (think of them as normal variables with strange names :P)

Here's one proc to remove elements from a list and one to unset array elements:

Code: Select all

# usage: lremove <list> <glob pattern matched against element names>
# returns: the new list
proc lremove {list mask} {
	set i 0
	foreach elem $list {
		if {[string match $mask $elem]} {set list [lreplace $list $i $i]} {incr i}
	}
	set list
}
# usage: array_unset <array name> <glob pattern matched against element names>
# returns: nothing
proc array_unset {array mask} {
	upvar 1 $array arr
	foreach elem [array names arr $mask] {unset arr($elem)}
}
Have you ever read "The Manual"?
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

user, what did you do to your avatar again..
Locked