Forum Moderators: phranque

Message Too Old, No Replies

need to sort a text file

         

indiandomain

3:31 am on Oct 20, 2003 (gmt 0)

10+ Year Member



hello
i have a text file that has a list of words like

widgets blue
widgets
red widgets for sale

now i want to sort the file according to the number of words
so output will be

widgets
widgets blue
red widgets for sale

does anyone have a solution for this?

-indiandomain

victor

7:22 am on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one in REBOL:

rebol []
mydata: read/lines %input-file
sort/compare mydata func [a b] [(length? parse a) < (length? parse b)]
write %output-file.txt mydata

indiandomain

9:03 am on Oct 20, 2003 (gmt 0)

10+ Year Member



thanks.
im not aware of rebol.
how do i use this script?

victor

10:06 am on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Download REBOL/core from www.rebol.com -- it's about 250K and it's free. It takes maybe 30 seconds to install.

If you are not running as a CGI on a webserver:

  • change the file names to your file names (%input-file.txt and %output-file.txt -- keep the percent sign). If you have a full path, use "unix-style" forward slashes even if you are running on windas (%/path1/path2/path3/input-file.txt)
  • Cut'n'paste the four lines of code from message 2 into a text editor and save as (say) sort-by-size.r
  • If you set an association between *.r and rebol.exe when you installed, simply double-click sort-by-size.r

    If you want to do this on a webserver as a CGI, you need to enable REBOL as a CGI process. Not difficult, but it needs a little fiddling witn Apache or IIS. See the FAQ on the REBOL site.

    It's a fun little language. Good luck!

  •