Version 2 of Shuffle a file

Updated 2008-10-01 14:43:30 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. The code needs to trim off the possible last empty element, perhaps setting a flag indicating whether or not there was a trailing newline.

Why bother with this? Sometimes having a set of data come into a program in a random order is useful for testing.

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 $str