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.

How to use lreplace in nested lists

Old posts that have not been replied to for several years.
Locked
Q
QKill
Voice
Posts: 6
Joined: Mon Aug 09, 2004 9:09 am
Location: Stockholm, Sweden
Contact:

How to use lreplace in nested lists

Post by QKill »

Hi,

how can i update an object in a nested list?

Code: Select all

set cal(0) [list {2} {2004-09-02} {19:00} {clame} {{{q3wcp14} {3} {14}} {{q3wcp17} {20} {1}}} {information} {5} {qkill} {blue} {tiny}]
If i for example want to update the value q3wcp17 which is in the third level of the nested list. Can anyone help me?

Thanks!

// Peter
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

use 'lset' if you're using one of the most recent versions of Tcl, if not, try this proc:

Code: Select all

proc lreplace2 {list path val} {
	set i [lindex $path 0]
	set path [lrange $path 1 end]
	if {[llength $path]} {
		lreplace $list $i $i [lreplace2 [lindex $list $i] $path $val]
	} {
		lreplace $list $i $i $val
	}
}
% lreplace2 {{{0 1 2} {0 1 2}} 2} {0 1 0} Hey?
{{0 1 2} {Hey? 1 2}} 2
Have you ever read "The Manual"?
Q
QKill
Voice
Posts: 6
Joined: Mon Aug 09, 2004 9:09 am
Location: Stockholm, Sweden
Contact:

Post by QKill »

Thanks alot!

// Peter
Locked