I have a .txt file which looks like this:
3 abcdsaaa
15 kljsfdfdfd
32 jdksdsds
6 khjaasa
15 dsdjsd
(it has some lines and the first [lindex [split $text] 0]] of all lines is a number)
Now I want to create a new .txt file where the first line would be this with the largest number, the second line would be this with the next number etc. For this example the file I want to be:
32 jdksdsds
15 dsdjsd
15 kljsfdfdfd
6 khjaasa
3 abcdsaaa
(If two numbers are the same I don't have any problem which line would be upper than other.
Sure... open the file, read in everything, close it, split it by line, then write a proc to compare two lines (using lindex like you have), and sort the whole thing with lsort -command yourcommand. Then open the file again for writing, write the new list.
You mean that when I read a line to put in the right position in another file??
smt like this:
set a [open $file1 r]
set b [open $file1 w+]
while {![eof $a]} {
gets $a linea
set numa [lindex [split $linea] 0]
while {![eof $fileb]} {
gets $b lineb
set numb [lindex [split $lineb] 0]
if {$numa > $numb} { puts $numb $linea }
# Here I lose the content of previous line?
}
}
Could you help me a little more with the lsort command?
You're right, -dict works too, I was just thinking of using the lsort -command option instead. You write a proc to compare 2 list items, returning -1, 0, or 1 (less than, equal, greater than). It's handy when you want to do complex sorting (like maybe you want to sort user handles by their flag levels, +n at the top etc).
You might want to change the way you read in the file though, because it will keep adding a new blank line at the end. This works a bit better: