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.

Tcl Script for Sorting lines in File

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
Z
Zircon
Op
Posts: 191
Joined: Mon Aug 21, 2006 4:22 am
Location: Montreal

Tcl Script for Sorting lines in File

Post by Zircon »

Hi all

i m lookin for a TCL script that can produce an output file by sorting the lines of an input file. The input file is a Trivia file.

Can some1 help me ? Thanks in advance
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Use the [lsort] Tcl-command to sort a list.
Z
Zircon
Op
Posts: 191
Joined: Mon Aug 21, 2006 4:22 am
Location: Montreal

Post by Zircon »

Hi all

Thanks Sir_Fz for this hint. Unfortnately i don't know tcl, but i have a piece of code that can be a good start if some 1 can help to modify it to what i want. I think this code sorts the input file and make everything in a sigle line, like if we give it a c b in separate lines, it will return a b c in a single line in the output file. Me, i want to sort the file, but keep them in separate lines.


proc trivsort {} {
putlog "starting sorting......."
set f [open "input.txt" r]
set triv [read $f]
close $f
set trivlist [split $triv "\n"]
unset triv
lsort -increasing $trivlist
set f [open "output.txt" w]
puts $f $trivlist
close $f
putlog "sorting done.."
}

Thanks in advance
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

proc trivsort {} {
 putlog "starting sorting......."
 set triv [lsort [split [read [set f [open input.txt]]] \n]][close $f]
 set f [open output.txt w]
 puts $f [join $triv \n]
 close $f
 putlog "sorting done.."
}
Z
Zircon
Op
Posts: 191
Joined: Mon Aug 21, 2006 4:22 am
Location: Montreal

Post by Zircon »

Hello Sir_Fz

Thanks very much for your help, your script is fully working :D
Post Reply