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

Inserting expression into list

Hello,

 

I'm learning to work with expression manipulations and I'm having a difficult time wrapping my head around it.

 

In the first section of the code below, I can successfully insert the string column name into the column() expression, but I cannot insert the generated expressions into a list.

 

The second section breaks apart interaction terms (this is an effect list from SLS), inserts them into their own column() expression, pieces them back together as an interaction term, and then inserts that into a list.  This section doesn't work.

 

Does anyone have any helpful tips or see something that I'm misunderstanding about how expression manipulations work?  Thank you in advanced!

 

names default to here(1);

// Basic need below - but only works for single column names
collist = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5" };

newlist = {};

for( i = 1, i <= N items(collist), i++,

	// Empty arg to identify cols by names
	name_arg = Expr( column() );

	insert into( name_arg, eval insert( collist[i] ) ); // result: column("Col 1"), or other cols inserted <-- Working as intended
	
	show( name_arg );  // Shows successful insert into's
	
	insert into( newlist, insert into( name_arg, eval insert( collist[i] ) ) );
	// Nested insert into doesn't work
	// Need to insert the result of the first insert into, into newlist

);

show( newlist ); // Currently shows empty list

/////////////////////////////////////////  DOES NOT WORK, BELOW

// Advanced flow - interaction detection 
collist2 = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 1*Col 2", "Col 2*Col 4", "Col 5*Col 5" };

newlist2 = {};

for( a = 1, a <= N items( collist2 ), a++,

	// Empty arg to identify cols by names
	name_arg = Expr( column() );
	
	// For whatever the match below, the result must be added to newlist2
	// Regex works, as per regex101.com
	if( !contains( collist2[a], "*" ), insert into( name_arg2, eval insert( collist2[a] ) ), // single term insert, refer to basic needs flow above
		insert into( name_arg2, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\1" ) ) ); // Breaks apart 1st group to insert into column()
		insert into( name_arg2, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\2" ) ) ); // Breaks apart 2nd group to insert into column()
		
		// The line below is a mockup of joining back together the first and second group together as interaction terms - doesn't work
		//eval expr( expr( insert into( name_arg2, eval insert( regex( mylist2[a], "^(.*)\*(.*)$", "\1" ) ) )*insert into( name_arg2, eval insert( regex( mylist2[a], "^(.*)\*(.*)$", "\2" ) ) );
	)
	
);

Desired_output = { column("Col 1"), column("Col 2"), column("Col 3"), column("Col 4"), column("Col 5"), column("Col 1")*column("Col 2"),
	column("Col 2")*column("Col 4"), column("Col 5")*column("Col 5") };
Learning every day!
2 ACCEPTED SOLUTIONS

Accepted Solutions
StarfruitBob
Level VI

Re: Inserting expression into list

The first section, where I'm wanting to insert expressions into a list, can easily be solved by the line of code below.

 

insert into( newlist, name expr( name_arg ) );

Onto solving the next section!

Learning every day!

View solution in original post

StarfruitBob
Level VI

Re: Inserting expression into list

Got the second part of the script to work, the one with interaction terms!

 

for( a = 1, a <= N items( collist2 ), a++,

	// Empty arg to identify cols by names
	name_arg = Expr( column() );
	name_arg2 = Expr( column() );
	
	// For whatever the match below, the result must be added to newlist2
	// Regex works, as per regex101.com
	if( !contains( collist2[a], "*" ), insert into( name_arg, eval insert( collist2[a] ) );
			insert into( newlist2, name expr( name_arg ) ), // single term insert, refer to basic needs flow above
		insert into( name_arg, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\1" ) ) ); // Breaks apart 1st group to insert into column()
		insert into( name_arg2, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\2" ) ) ); // Breaks apart 2nd group to insert into column()
		insert into( newlist2, Eval Expr( expr( name_arg )*expr( name_arg2 ) ) );

	);
	
);

show( newlist2 );

Output:
newlist2 = {Column("Col 1"), Column("Col 2"), Column("Col 3"), Column("Col 4"), Column("Col 5"), Column("Col 1") * Column("Col 2"), Column("Col 2") * Column("Col 4"), Column("Col 5") * Column("Col 5")};

Hopefully this helps guide others learning expression manipulations. It's a long journey - but it's powerful and worth it!

Learning every day!

View solution in original post

2 REPLIES 2
StarfruitBob
Level VI

Re: Inserting expression into list

The first section, where I'm wanting to insert expressions into a list, can easily be solved by the line of code below.

 

insert into( newlist, name expr( name_arg ) );

Onto solving the next section!

Learning every day!
StarfruitBob
Level VI

Re: Inserting expression into list

Got the second part of the script to work, the one with interaction terms!

 

for( a = 1, a <= N items( collist2 ), a++,

	// Empty arg to identify cols by names
	name_arg = Expr( column() );
	name_arg2 = Expr( column() );
	
	// For whatever the match below, the result must be added to newlist2
	// Regex works, as per regex101.com
	if( !contains( collist2[a], "*" ), insert into( name_arg, eval insert( collist2[a] ) );
			insert into( newlist2, name expr( name_arg ) ), // single term insert, refer to basic needs flow above
		insert into( name_arg, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\1" ) ) ); // Breaks apart 1st group to insert into column()
		insert into( name_arg2, eval insert( regex( collist2[a], "^(.*)\*(.*)$", "\2" ) ) ); // Breaks apart 2nd group to insert into column()
		insert into( newlist2, Eval Expr( expr( name_arg )*expr( name_arg2 ) ) );

	);
	
);

show( newlist2 );

Output:
newlist2 = {Column("Col 1"), Column("Col 2"), Column("Col 3"), Column("Col 4"), Column("Col 5"), Column("Col 1") * Column("Col 2"), Column("Col 2") * Column("Col 4"), Column("Col 5") * Column("Col 5")};

Hopefully this helps guide others learning expression manipulations. It's a long journey - but it's powerful and worth it!

Learning every day!