File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ package require Tcl 8.6
2+ package require struct::matrix
3+
4+ proc usage {} {
5+ puts stderr {Usage: please enter the dimension of the matrix and the serialized matrix}
6+ exit 1
7+ }
8+
9+ if {$argc != 3} { usage }
10+ set cols [lindex $argv 0]
11+ set rows [lindex $argv 1]
12+ set matrixStr [lindex $argv 2]
13+
14+ if {$cols eq " " || $rows eq " " || $matrixStr eq " " } { usage }
15+
16+ if {![string is integer -strict $cols ] || ![string is integer -strict $rows ]} {
17+ usage
18+ }
19+
20+ set values [lmap v [split $matrixStr " ," ] {string trim $v }]
21+ if {[llength $values ] != $cols * $rows } { usage }
22+
23+ set m [struct::matrix]
24+ $m add columns $cols
25+ $m add rows $rows
26+
27+ for {set r 0} {$r < $rows } {incr r} {
28+ set start [expr {$r *$cols }]
29+ set end [expr {($r +1)*$cols - 1}]
30+ $m set row $r [lrange $values $start $end ]
31+ }
32+
33+ $m transpose
34+
35+ set outList {}
36+ for {set i 0} {$i < $cols } {incr i} {
37+ lappend outList {*}[$m get row $i ]
38+ }
39+
40+ puts [join $outList " , " ]
41+
You can’t perform that action at this time.
0 commit comments