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.

"Sort" a file!

Old posts that have not been replied to for several years.
Locked
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

"Sort" a file!

Post by cerberus_gr »

Hello,

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.

Any ideas?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

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.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

why would he need to compare the lines? all he wants to do is sort them by decreasing order
Elen sila lúmenn' omentielvo
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

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?
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

Papillon a bit more help? :P does it works if the numbers are:
3, 12, 32 or I should have 03, 12, 32?
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

set fid [open $file r]
while {"[eof $fid]} {
  gets $fid line
  lappend somelist $line
}
catch {close $fid}
set xid [open $file w]
set somelist [lsort -dict -decreasing $somelist]
foreach x $somelist {
  puts $xid $x
}
catch {close $xid}
something like this
Elen sila lúmenn' omentielvo
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

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:

Code: Select all

set fid [open $file r]
set somelist [split [read -nonewline $fid] \n]
close $fid
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

yeah, go with stdragons solution, forgot about the blank lines :oops:
Elen sila lúmenn' omentielvo
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

Thank you both guys. The procedure that I have written was 46 lines :oops: and now it's 10 lines only and of course works better :D :D
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Since the output will be the exact same size as the input you don't need to create a new file (using 'open' twice).

Open it using r+ and seek back to the start before writing the output:

Code: Select all

set file "sortme.txt"
puts -nonewline [set f [open $file r+]] [join [lsort -dict -decr [split [read $f][seek $f 0] \n]] \n]
close $f
excuse my weird syntax :)
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

Thx user. It works great, even if the syntax is weird :P
Locked