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

Fill column with values based on another column with keys and associative array

I did a quick search and couldn't find a solution. I solved it in a pretty complex way some time ago, but was wondering if there is an easier way.

Problem: I have an associative array of a form:

assarr = ["AA" => "AAA", "BB" => "BBB", => "CCC" ];

I have a column "A" that has either "AA", "BB", or something else.

I need to fill column "B" with values based on the associative array and column "A". i.e. for a row with "A" having "AA", "B" should be "AAA" and so on. If "A" has values not in the array, "B" should get the default "CCC".

 

Based on the fact how easy it is to create an associative array from two columns, there should be equally easy way to fill column based on associative array and another column with keys.

 

Thanks, 

M.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Fill column with values based on another column with keys and associative array

Do you mean something like this?

Names Default To Here(1);

aa = ["AA" => "AAA", "BB" => "BBB", => "CCC"];

dt = New Table("Untitled",
	Add Rows(5),
	Compress File When Saved(1),
	New Column("Column 1",
		Character,
		"Nominal",
		Set Values({"AA", "BB", "aa", "C", "D"})
	)
);

wait(1);

dt << New Column("Column 2", Character, Nominal, << Set Each Value(
	aa[:Column 1]
));
-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: Fill column with values based on another column with keys and associative array

Do you mean something like this?

Names Default To Here(1);

aa = ["AA" => "AAA", "BB" => "BBB", => "CCC"];

dt = New Table("Untitled",
	Add Rows(5),
	Compress File When Saved(1),
	New Column("Column 1",
		Character,
		"Nominal",
		Set Values({"AA", "BB", "aa", "C", "D"})
	)
);

wait(1);

dt << New Column("Column 2", Character, Nominal, << Set Each Value(
	aa[:Column 1]
));
-Jarmo
miguello
Level VI

Re: Fill column with values based on another column with keys and associative array

Hmmm. Yes, this is exactly what I need. Thank you! Somehow I didn't try this rather obvious approach before.