creating tcl list of first 2 column of matrix

I have matrix of 10 rows and 10 column. I want to crate tcl list where each element of list will be 2 numbers..1 from each row. e.g if My matrix is

$a $b $c $d $e  
$f $g $h $i $j  
$k $l $m $n $o  
$p $q $r $s $t  

I want to have list that contain elements $a $b, $f $g, $k $l, $p $q. Can someone tell me how to do this ?

Upvotes: 0

Views: 1107

Answers (3)

syox
syox

Reputation: 3

package require struct::matrix 

struct::matrix data
data add columns 5
data add rows    4

data set rect 0 0 {
{a b c d e}
{f g h i j}
{k l m n o}
{p q r s t}
}

data get rect 0 0 1 end
# {a b} {f g} {k l} {p q}

This should produce the results

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137787

If you are using a matrix as defined by the struct::matrix package in Tcllib, you do this:

set pairlist [$matrix get rect  0 0  1 end]

Notes: the name of the matrix is in the matrix variable, rect is short for “rectangle”, the 0 0 give the coordinates in the matrix of the top-left corner of the rectangle to extract, and 1 end gives the coordinates in the matrix of the bottom right corner of the rectangle (matrices support end to mean the last row and/or column, just like Tcl strings and lists).

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247202

Assuming your matrix is a list of lists, you can use the lmap command:

$ tclsh
% set matrix {
{a b c d e}
{f g h i j}
{k l m n o}
{p q r s t}
}

{a b c d e}
{f g h i j}
{k l m n o}
{p q r s t}

% lmap sublist $matrix {lrange $sublist 0 1}
{a b} {f g} {k l} {p q}

Upvotes: 1

Related Questions