cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
IvanK
Level II

Column by index

Hi! 
What's the right way to reference a column by index? Ideally I was looking for something like this:

IvanK_1-1623867511998.png

Thank you in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Column by index

Ah... I see what you want now.

 

Try this as your formula. It uses the format column(dt, n)[row()] to get the value in the current row, of the nth column of a data table, where the value of n is whatever you've placed in the current row of the :block column.

 

Column( :Block << get data table, :Block )[Row()]

Cheers,

Brady

View solution in original post

7 REPLIES 7
jthi
Super User

Re: Column by index

You should be able to use just Column(index).

 

Send Messages to Data Column Objects 

 

col = Column( 2 );     // assign a reference to the second column
-Jarmo
IvanK
Level II

Re: Column by index

It may be true for scripts, but in formulas I get this error:

IvanK_0-1623872326795.png

Interestingly, the popup suggests thatthe number can be provided, but it doesn't appear to be the case

IvanK_1-1623872374721.png

 

jthi
Super User

Re: Column by index

Try with << get name instead of Column Name()

 

Choose(:Block, 
	Column(1) << get name, 
	Column(2) << get name, 
	Column(3) << get name
)

jthi_0-1623930828018.png

 

Full example:

 

Names Default To Here(1);


dt = New Table("Untitled",
	Add Rows(3),
	Compress File When Saved(1),
	New Column("Block", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2, 3])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([1, ., .])),
	New Column("Column 3", Numeric, "Continuous", Format("Best", 12), Set Values([2, ., .])),
	New Column("Column 4", Numeric, "Continuous", Format("Best", 12), Set Values([3, ., .])),
	New Column("Formula",
		Character,
		"Nominal",
		Formula(Choose(:Block, Column(1) << get name, Column(2) << get name, Column(3) << get name))
	)
);
-Jarmo
IvanK
Level II

Re: Column by index

Thank you jthi, I feel like "<< get name" gets us one step forward. My final goal is to do a HLOOKUP basically, here's a demonstration. Currently it pulls the column name, while I want it to look at the data of that column. 

 

IvanK_3-1623931986676.png

Here's full code if needed: 

New Table( "Untitled",
Add Rows( 3 ),
Compress File When Saved( 1 ),
New Column( "Column 1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [11, 111, 1111] )),
New Column( "Column 2",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [22, 222, 2222] )),
New Column( "Column 3",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [33, 333, 3333] )
),
New Column( "Block",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 2, 3] )
),
New Column( "Formula",
Character,
"Nominal",
Formula(
Choose( :Block,
Column( 1 ) << get name,
Column( 2 ) << get name,
Column( 3 ) << get name))))
 

 

jthi
Super User

Re: Column by index

Not exactly sure what data you want to get from the columns. Does Block column define the column and row for the cell you want to get the data from?

 

Column(:Block)[:Block]

jthi_0-1623932861535.png

 

-Jarmo

Re: Column by index

Ah... I see what you want now.

 

Try this as your formula. It uses the format column(dt, n)[row()] to get the value in the current row, of the nth column of a data table, where the value of n is whatever you've placed in the current row of the :block column.

 

Column( :Block << get data table, :Block )[Row()]

Cheers,

Brady

IvanK
Level II

Re: Column by index

Thank you Brady, this is exactly what I was looking for. Much obliged!