For Programmers: Free Programming Magazines  


Home > Archive > Tcl > July 2007 > files created, filled, incremented









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author files created, filled, incremented
ninoadrien7171@gmail.com

2007-07-22, 4:28 am

Hi all

Just a question about a simple programme :

The proc splite should make a loops putting each text's fragments
(4 items) on different files : idfile

But surprise when i notice that it don't put each text fragments in
different file ($Txt_$x.txt)

but more it don't attribute a number for each ($x)!

I have no idea of what happened in this programmes ? Could you help me
please ?

This is the programme :

set about "minEd - a minimal editor
Richard Suchenwirth 2003
F1: help
F2: load
F3: save
"
pack [scrollbar .y -command ".t yview"] -side right -fill y
pack [text .t -wrap word -setgrid 1 -yscrollc ".y set" -undo 1] -side
right -fill both -expand 1
pack [button .b -text click -bg GreenYellow -fg black -command
splite] -fill both -expand 1

bind . <F1> {tk_messageBox -message $about}
bind . <F2> {loadText .t [tk_getOpenFile]}
bind . <F3> {saveText .t [tk_getSaveFile]}

proc splite {} {
set ::texte [.t get 1.0 end]

set _texte [string map {"\n" ""} $::texte]
# transforme string to list
set _liste [split $_texte]
# cut each 4 items
set _resultat [list]
set _noligne 1
set Txt [wm title .]
set x 1
set Idfile [ file mkdir /home/tomtom/$Txt_$x.txt/ ; open /home/
tomtom/$Txt_$x.txt/ w+]

foreach _item $_liste \
{
if {$_item ne ""} \
{
lappend $_resultat $_item

if {$_noligne % 4 == 0} \
{
puts $Idfile [join $_resultat]

}
incr _noligne
incr x 1
}
}
}

proc loadText {w fn} {
if {$fn==""} return
wm title . [file tail $fn]
set fp [open $fn]
$w delete 1.0 end
$w insert end [read $fp]
close $fp
}

proc saveText {w fn} {
if {$fn==""} return
set fp [open $fn w]
puts -nonewline $fp [$w get 1.0 "end - 1 c"]
close $fp
}

if {$argc > 0} {
loadText .t [lindex $argv 0]
} else {
.t insert end "Keys: F1:Help, F2:Open, F3:Save\n"
}
focus -force .t

Stéphane A.

2007-07-22, 4:28 am

> set x 1
> set Idfile [ file mkdir /home/tomtom/$Txt_$x.txt/ ; open /home/
> tomtom/$Txt_$x.txt/ w+]
>
> foreach _item $_liste \
> {
> if {$_item ne ""} \
> {
> lappend $_resultat $_item
>
> if {$_noligne % 4 =3D=3D 0} \
> {
> puts $Idfile [join $_resultat]
>
> }
> incr _noligne
> incr x 1
> }
> }
>
> }
>

It seems that the [open] call is outside the foreach loop. $x gets
incremented within that loop, but never gets used.

Regards,
St=E9phane

Glenn Jackman

2007-07-23, 8:08 am

At 2007-07-22 12:18AM, "ninoadrien7171@gmail.com" wrote:
> set Txt [wm title .]
> set Idfile [ file mkdir /home/tomtom/$Txt_$x.txt/ ; open /home/
> tomtom/$Txt_$x.txt/ w+]
>
> foreach _item $_liste \
> {
> if {$_item ne ""} \
> {
> lappend $_resultat $_item
>
> if {$_noligne % 4 == 0} \
> {
> puts $Idfile [join $_resultat]
>
> }
> incr _noligne
> incr x 1
> }
> }


Many things wrong here...

You create a directory and then try to open it as a file.

You open the file outside of the foreach loop, so your $x counter is
never used.

Also, note that $Txt and $Txt_ are different variables. A variable name
is "a sequence of one or more characters that are a letter, digit,
underscore, or namespace separators (two or more colons)."
http://www.tcl.tk/man/tcl8.4/TclCmd/Tcl.htm#M11

Try this:

foreach _item $_liste {
set Idfilename /home/tomtom/${Txt}_$x.txt
set channel [open $Idfilename w+]
# ...
puts $channel ...
# ...
close $channel
incr x
}

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com