#! /bin/bash # \ export TCL_LIBRARY=$HOME/tcl/lib/tcl ; \ exec /usr/local/bin/tclsh $0 "$@" # My super globals # array set Env \ [list \ {cFile} {countries.tcl} \ {numofcountries} 2 \ ] # Read in the countries list. # source $Env(cFile) # Create a mapping of lowercase, "stripped" country names to the orginal names. # foreach c [array names country] { # # Modulate to lowercase and remove...: # # o Blanks # o Apostrophes (Cote D'Ivoire) # regsub -all {[ ']} [string tolower $c] {} d set realname($d) $c } # Now create the master global list list of lowercase, stripped names. # set globclist [lsort [array names realname]] set globclistlen [llength $globclist] # We recursively loop over subsets of the global country list, # putting together unique country sets for analysis. # proc addcountry {clist moretoadd} { global Env globclist globclistlen solutions realname # Do we not yet have a complete list of countries on this recursive call? # if {$moretoadd} { # # Yes; there are still more to add. # # Output the first country in the list for visual feedback. # if {$moretoadd == ($Env(numofcountries) - 1)} { puts [lindex $clist 0] } # Create this level's country list to loop over, and loop over it. # for \ {set countryix \ [expr { [llength $clist] ? [expr [lindex $clist end] + 1] : 0}]} \ {$countryix < ($globclistlen - $moretoadd - 1)} \ {incr countryix} { # # Recursively call this routine with each new country. # addcountry \ [concat $clist [list $countryix]] \ [expr $moretoadd - 1] } } else { # # No; no more to add. # We have all our countries lined up for analysis. # # Start by creating our key string which will be sorted. # foreach c $clist { append thiskey [lindex $globclist $c] } # Now create our list of solutions keyed by sorted characters; # This automatically puts our solutions together. # # Note that we're keeping our numeric country codes in the data field # 'til the very end. This conserves (a little) memory, and allows # us to work with indexes, which should also save us a little time. # lappend solutions([join [lsort [split $thiskey {}]] {}]) $clist } return } # Kick it off. # addcountry {} $Env(numofcountries) # Throw out single (useless) "solutions". # foreach {ix dat} [array get solutions] { if {[llength $dat] < 2} { unset solutions($ix) } } # Show the solutions! # foreach {ix dat} [array get solutions] { foreach countrylist $dat { set thissol {} foreach thiscountry $countrylist { lappend thissol $realname([lindex $globclist $thiscountry]) } puts $thissol } puts {} } exit 0