[HJG] 2012-12-29 - Demo for re-formatting string to list. ====== # http://wiki.tcl.tk/37505 # for-mat2.tcl - HaJo Gurt - 2012-12-29 #: Re-Format long string to a list of filenames/URLs # e.g. to be used with "wget -i file" set Hdr "For-Mat2" ####+####1####+####2####+####3####+####4####+####5####+####6####+####7####+### ## Console-settings, to allow running in wish as well as in tclsh: catch { console show } catch { wm withdraw . } # catch { console eval { .console config -bg grey -fg blue } } # Resize & put at top-right corner on 1024x768: ##catch { console eval { wm geometry . 56x12+550+0 } } catch { console title "$Hdr" } proc q {} { exit } ####+####1####+####2####+####3####+####4####+####5####+####6####+####7####+### puts "# $Hdr:" # set PL {bild01.jpg bild02.jpg bild03.jpg} ## # set P1 "http://blah.de/fasel/" # set P2 "bild01.jpg" ## # set url "http://blah.de/fasel/bild01.jpg" set P1 "http://blah.de/fasel/" set PS {bild01.jpg,bild02.jpg,bild03.jpg} puts "# PS=$PS." set PL [string map -nocase { "," " "} $PS] puts "# PL=$PL." set i 0 foreach P2 $PL { incr i set url "$P1$P2" set filename "XY_$P2" # puts "$i. URL=$url." # puts "filename=$filename." puts $url } puts "# Done." #. ====== Output: ====== # For-Mat2: # PS=bild01.jpg,bild02.jpg,bild03.jpg. # PL=bild01.jpg bild02.jpg bild03.jpg. http://blah.de/fasel/bild01.jpg http://blah.de/fasel/bild02.jpg http://blah.de/fasel/bild03.jpg # Done. ====== [JJS] 2013-01-04 - [[string map]] doesn't convert a string to a list. In your particular example, it converts a string into another string that can be automagically interpreted as a list of multiple items, if needed. What you want is ====== set PL [split $PS ,] ====== which actually converts a string to a list. Furthermore, your [[string map]] approach is susceptible to all the usual problems that can arise when you treat a string as a list: 1. If the items contain spaces, then the number of list elements is thrown off. 1. If the items contain braces, then the automagic conversion may fail. For example, ====== (System32) 69 % set PS {{one,two,}three} {one,two,}three (System32) 70 % set PL [string map {"," " "} $PS] {one two }three (System32) 71 % lindex $PL 0 list element in braces followed by "three" instead of space (System32) 72 % set PL [split $PS ,] \{one two \}three (System32) 73 % lindex $PL 0 {one ====== ---- '''See also:''' [for], [foreach], [I love foreach], [string], [regexp], [regsub], [Regular Expression Examples] <>Example