Version 1 of Shuffle a file

Updated 2008-10-01 14:37:42 by LV

Based on code from shuffle a list, here's a first draft of a program that shuffles a file - read in the file, turn it into a list, shuffle the list, then output the lines in the random order.

Note that from my initial test, the code is not right yet. For some reason, the first line is not being output.

if { $::argc == 0 } {
	puts stderr "USAGE: $::argv0 filename"
	return 0
}

set fd [open [lindex $::argv 0] "r"]
set str [read $fd]
set lst [split $str "\n"]

 proc shuffle10a list {
    set len [llength $list]
    while {$len} {
	set n [expr {int($len*rand())}]
	set tmp [lindex $list $n]
	lset list $n [lindex $list [incr len -1]]
	lset list $len $tmp
    }
    return $list
 }

set str [join [shuffle10a $lst] "\n"]
puts -nonewline $str