Choose Language Hide Translation Bar
Highlighted
Community Trekker

## Assigning Column Name (x) output to a matrix?

As part of a longer script, I would like to extract the column names from one table and list those in a new table in a new column.

I have naively tried the following:

dt = Current Data Table ();

m = [];

For(i=8,i<= N Col (dt),i++,

m = m || Column Name ( i );

);

New Table ("Table", New Column ("Headers", character,set values(m)));

I get a error message "Concatenate for Strings or Matrices Only" which make sense since I'm trying to stick a Name into a Matrix.

What is the proper syntax and/or function to make this work?

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Manager

## Re: Assigning Column Name (x) output to a matrix?

Use a List instead a Matrix​, and then use the Insert Into() function.

`dt = Current Data Table ();m = {};For(i=8,i<= N Col (dt),i++,insert into(m,  Column Name ( i )););New Table ("Table", New Column ("Headers", character,set values(m)));`

The For() loop may be inefficient for large data tables. In that case a modification of txnelson​'s solution may be better. This version indexes the list returned by Get Column Names to use the items from 8 to the end.

`Names Default to Here(1);dt = Current Data Table();dt2 = New Table( "Table", New Column( "Headers", character, values( (dt << get column names)[8 :: N Items( dt << get column names )] ) ));`
-Jeff
3 REPLIES 3
Highlighted
Super User

## Re: Assigning Column Name (x) output to a matrix?

I believe this makes it a little simpler

Names Default to Here(1);

dt = Current Data Table();

dt2 = New Table("Table",New Column("Headers", character, values(dt<<get column names)));

Jim
Highlighted
Community Trekker

## Re: Assigning Column Name (x) output to a matrix?

Hi Jim,

Thank you for your elegant suggestion.

While your script captures all the column names from one file, I would like to be able to skip the first 8 columns to only capture the names of the columns on which I'm computing some stats.

Sincerely,

Thierry

Highlighted
Community Manager

## Re: Assigning Column Name (x) output to a matrix?

Use a List instead a Matrix​, and then use the Insert Into() function.

`dt = Current Data Table ();m = {};For(i=8,i<= N Col (dt),i++,insert into(m,  Column Name ( i )););New Table ("Table", New Column ("Headers", character,set values(m)));`

The For() loop may be inefficient for large data tables. In that case a modification of txnelson​'s solution may be better. This version indexes the list returned by Get Column Names to use the items from 8 to the end.

`Names Default to Here(1);dt = Current Data Table();dt2 = New Table( "Table", New Column( "Headers", character, values( (dt << get column names)[8 :: N Items( dt << get column names )] ) ));`
-Jeff