flatten out keys to map to values in kdb

Consider following table in kdb:

`Keys `Values`A`B `V1`C`D`E `V2 

I want to flatten out Keys to map to corresponding value.that is, transform to:

`Key `Values`A `V1`B `V1`C `V2`D `V2`E `V2

Is there a succinct and easy way of doing this over looping over each entry using each?

Alternatively,
 is there a way to query part of key?
e.g. in the following table

`Keys `Values`A`B `V1`C`D`E `V2

Lookup for A should return V1

q)g:((Keys;Values);(AB;V1);(CDE;`V2))
q)(!). flip raze(,'/).'g,::()
Keys| Values
A   | V1
B   | V1
C   | V2
D   | V2
E   | V2

I’ve no idea what you did but it works.
How do you make it work if g is table

`Keys `Values`A`B `V1`C`D`E `V2

<o:p> </o:p>

Easy.<o:p></o:p>

<o:p> </o:p>

ungroup flip KeysValues!flip((AB;V1);(CDE;`V2))<o:p></o:p>

<o:p> </o:p>

Kim<o:p></o:p>

<o:p> </o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Sam
Gesendet: Freitag, 2. September 2016 21:13
An: Kdb+ Personal Developers
Betreff: [personal kdb+] Re: flatten out keys to map to values in kdb<o:p></o:p>

<o:p> </o:p>

I’ve no idea what you did but it works.
How do you make it work if g is table <o:p></o:p>

`Keys  `Values

`A`B   `V1

`C`D`E   `V2<o:p></o:p>

here is another challenge.
what is values are also composite

g:(k:((AB);(CDE));v:((V1V2);(V3`V4)))
k      v

AB   V1 V2
CD`E V3 V4

ungroup doesn’t work

The result should be
k      v

A&nbsp;&nbsp; V1 V2 B   V1 V2
C&nbsp;&nbsp; V3 V4 D   V3 V4
`E   V3 V4

(k:raze gk;v:raze (count each gk)#'enlist each g`v)

Nice. Also “where” works nicely instead of "count each… # '…):

flip kv!(raze k;g[v] where count each k:gk)

or

select raze k,v:v where count each k from g

Cheers

Ryan

On Sunday, September 4, 2016 at 7:14:22 AM UTC-4, James Little wrote:

(k:raze gk;v:raze (count each gk)#'enlist each g`v)